欧拉计划第三题

Euler 3. Largest prime factor
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
即是求数字600851475143的最大质因子。

Ruby代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class 
def max_prime(num)
a = 2
while a < num
if num%a ==0
num /= a
else
a +=1
end
end
puts "the largest prime factor is #{num} "
end
end
el = Euler_3rd.new
el.max_prime(600851475143)

输出结果

1
the largest prime factor is 6857