Created
July 1, 2012 22:41
-
-
Save pjc/3029906 to your computer and use it in GitHub Desktop.
Euler Project in Ruby - Problem 5
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
upto = 20 | |
def prime? x | |
(2..x-1).each { |y| return false if x % y == 0 } | |
true | |
end | |
# For speed reasons we want to find max possible increment | |
def increment upto | |
a = 1 | |
(1..upto).each { |x| a*= x if prime? x } | |
a | |
end | |
def no_remainder? x, upto | |
(1..upto).each { |y| return false if x % y != 0 } | |
true | |
end | |
increment = increment upto | |
x = increment | |
try_number = 1 | |
while true | |
if no_remainder? x, upto | |
puts "The smallest number is #{x}" | |
break | |
end | |
try_number += 1 | |
x = try_number * increment | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another clean and quick thing in Ruby.