TheAlgorithms-Ruby/project_euler/problem_3/sol2.rb
Malte Jürgens 2ea0f9aacf
Revert "remove project_euler"
This reverts commit cdc053662d.
2021-05-11 18:41:11 +02:00

18 lines
324 B
Ruby

# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number 600851475143 ?
def solution(n)
prime = 1
i = 2
while i * i <= n
while (n % i).zero?
prime = i
n = n.fdiv i
end
i += 1
end
prime = n if n > 1
prime.to_i
end
puts solution(600_851_475_143)