如何优化慢Ruby计算在项目欧拉中的性能?

2026-04-28 02:131阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计598个文字,预计阅读时间需要3分钟。

如何优化慢Ruby计算在项目欧拉中的性能?

这个问题要求找出最小的正整数,它能被从1到10的所有数整除。这是一个典型的数学问题,可以通过编程来解决。以下是简化后的开头和内容:

---

这个问题使用了Project Euler问题5,需要细心地解决!问题5问:2520是能被从1到10的所有数整除的最小数。那么,最小的正数,它能被1到10的所有数整除是什么?

---

这个问题描述了一个寻找最小公倍数(LCM)的问题。在Python中,可以使用数学模块中的`lcm`函数来求解。以下是可能的Python代码实现:

pythonfrom math import gcd

计算两个数的最大公约数def gcd_of_two(a, b): while b: a, b=b, a % b return a

计算多个数的最小公倍数def lcm_of_list(numbers): current_lcm=numbers[0] for num in numbers[1:]: current_lcm=current_lcm * num // gcd_of_two(current_lcm, num) return current_lcm

计算1到10的最小公倍数lcm_result=lcm_of_list(range(1, 11))print(fThe smallest positive number that is evenly divisible by each of the numbers from 1 to 10 is: {lcm_result})

这段代码会输出:

如何优化慢Ruby计算在项目欧拉中的性能?

The smallest positive number that is evenly divisible by each of the numbers from 1 to 10 is: 2520

这个问题引用了 Project Euler Problem 5,所以要小心剧透!
问题5:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

我在Ruby中编写了以下代码作为问题5的解决方案.

num = 2520 until (1..20).all?{|x| num % x == 0} num += 1 end puts "#{num}"

但是,每当我运行脚本时,它就会挂起.请注意,我在基础案例2520上测试了相同的方法,范围为1到10,并且它工作得很好.

为什么它适用于更简单的情况,但不适用于更高级的情况?我该怎么做才能解决我的问题?

你将无法像对待别人一样强行解决这个问题.您将需要为它找到更有效的解决方案.

下面的SPOILER

这是一种非常有效的方法(如果这不像Ruby那样道歉):

def find_multiple lcm = 1 (2..20).each do |i| lcm *= i / gcd(lcm, i) end lcm end def gcd(a, b) while b > 0 a %= b return b if a == 0 b %= a end a end puts find_multiple

如果您正在寻找更类似Ruby的方法来解决它,您可以使用以下内容(如评论中的steenslag所示):

(1..20).inject(:lcm)

本文共计598个文字,预计阅读时间需要3分钟。

如何优化慢Ruby计算在项目欧拉中的性能?

这个问题要求找出最小的正整数,它能被从1到10的所有数整除。这是一个典型的数学问题,可以通过编程来解决。以下是简化后的开头和内容:

---

这个问题使用了Project Euler问题5,需要细心地解决!问题5问:2520是能被从1到10的所有数整除的最小数。那么,最小的正数,它能被1到10的所有数整除是什么?

---

这个问题描述了一个寻找最小公倍数(LCM)的问题。在Python中,可以使用数学模块中的`lcm`函数来求解。以下是可能的Python代码实现:

pythonfrom math import gcd

计算两个数的最大公约数def gcd_of_two(a, b): while b: a, b=b, a % b return a

计算多个数的最小公倍数def lcm_of_list(numbers): current_lcm=numbers[0] for num in numbers[1:]: current_lcm=current_lcm * num // gcd_of_two(current_lcm, num) return current_lcm

计算1到10的最小公倍数lcm_result=lcm_of_list(range(1, 11))print(fThe smallest positive number that is evenly divisible by each of the numbers from 1 to 10 is: {lcm_result})

这段代码会输出:

如何优化慢Ruby计算在项目欧拉中的性能?

The smallest positive number that is evenly divisible by each of the numbers from 1 to 10 is: 2520

这个问题引用了 Project Euler Problem 5,所以要小心剧透!
问题5:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

我在Ruby中编写了以下代码作为问题5的解决方案.

num = 2520 until (1..20).all?{|x| num % x == 0} num += 1 end puts "#{num}"

但是,每当我运行脚本时,它就会挂起.请注意,我在基础案例2520上测试了相同的方法,范围为1到10,并且它工作得很好.

为什么它适用于更简单的情况,但不适用于更高级的情况?我该怎么做才能解决我的问题?

你将无法像对待别人一样强行解决这个问题.您将需要为它找到更有效的解决方案.

下面的SPOILER

这是一种非常有效的方法(如果这不像Ruby那样道歉):

def find_multiple lcm = 1 (2..20).each do |i| lcm *= i / gcd(lcm, i) end lcm end def gcd(a, b) while b > 0 a %= b return b if a == 0 b %= a end a end puts find_multiple

如果您正在寻找更类似Ruby的方法来解决它,您可以使用以下内容(如评论中的steenslag所示):

(1..20).inject(:lcm)