Skip to content

Instantly share code, notes, and snippets.

@theycallmeswift
Created March 8, 2012 23:31
Show Gist options
  • Save theycallmeswift/2004121 to your computer and use it in GitHub Desktop.
Save theycallmeswift/2004121 to your computer and use it in GitHub Desktop.
Calculate the first factorial where the digits sum to 8001
#!/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