Last active
December 31, 2015 05:19
-
-
Save CharlieTruong/7940170 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def next_prime(number) | |
prime=false | |
while prime==false | |
number+=1 | |
for i in 2..number | |
prime=true if i==number | |
break if number % i ==0 | |
end | |
end | |
return number | |
end | |
def prime_factor(number) | |
factors=Array.new | |
prime=2 | |
while prime<=number | |
if number % prime==0 | |
until number % prime != 0 | |
number=number/prime | |
factors.push(prime) | |
end | |
end | |
prime=next_prime(prime) | |
end | |
return factors.max | |
end | |
p prime_factor(600851475143) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment