mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-29 22:24:07 +01:00
Add the third problem from project Euler
Add the third problem from project Euler
This commit is contained in:
parent
aa4dfbeefb
commit
3cca135e4e
1 changed files with 19 additions and 0 deletions
19
Project Euler/Problem 3/problem3_sol1.rb
Normal file
19
Project Euler/Problem 3/problem3_sol1.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
# 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(600851475143)
|
||||
|
Loading…
Reference in a new issue