Skip to content

Instantly share code, notes, and snippets.

@michaeltwofish
Created February 20, 2011 07:18
Show Gist options
  • Save michaeltwofish/835790 to your computer and use it in GitHub Desktop.
Save michaeltwofish/835790 to your computer and use it in GitHub Desktop.
Euler Problem 3
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