Created
July 17, 2013 15:08
-
-
Save elreimundo/6021442 to your computer and use it in GitHub Desktop.
A friend posted a math puzzle: "Find the smallest integer that ends in 56, is divisible by 56, and whose digits add to 56." Here's the Ruby script I wrote to solve the puzzle.
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
def digitsum num | |
currentsum = 0 | |
(Math.log(num,10).ceil).downto(0) do |digit| | |
currentsum = currentsum + num/(10**digit) | |
num = num%(10**digit) | |
end | |
currentsum | |
end | |
leading_digits = 1 | |
while true | |
if (leading_digits*100+56)%56 == 0 && digitsum(leading_digits)+11 == 56 | |
break | |
else | |
leading_digits = leading_digits+1 | |
end | |
end | |
puts leading_digits.to_s + '56' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment