Skip to content

Instantly share code, notes, and snippets.

@elreimundo
Created July 17, 2013 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elreimundo/6021442 to your computer and use it in GitHub Desktop.
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.
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