Created
March 8, 2012 23:31
-
-
Save theycallmeswift/2004121 to your computer and use it in GitHub Desktop.
Calculate the first factorial where the digits sum to 8001
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
#!/usr/bin/env ruby | |
def factorial(n) | |
n == 0 ? 1 : n * factorial(n-1) | |
end | |
def R(n) | |
fact = factorial(n) | |
fact.to_s.split("").inject(0) { |sum, n| sum + n.to_i } | |
end | |
def find_smallest_factorial_sum(sum, n = 0) | |
R(n) == sum ? n : find_smallest_factorial_sum(sum, n+1) | |
end | |
puts find_smallest_factorial_sum(8001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment