-
-
Save michaeltwofish/835790 to your computer and use it in GitHub Desktop.
Euler Problem 3
This file contains hidden or 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
num = 600851475143 | |
# Brute force | |
def is_prime?(n) | |
limit = Math.sqrt(n).round | |
(2..limit).each do |i| | |
return false if n % i == 0 | |
end | |
return true | |
end | |
def is_factor?(num, n) | |
num % n == 0 | |
end | |
largest_prime = nil | |
Math.sqrt(num).round.downto 1 do |n| | |
if is_factor?(num, n) && is_prime?(n) | |
largest_prime = n | |
break | |
end | |
end | |
puts "Brute force: #{largest_prime}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment