Sum of digits in n!
A Ruby function to sum all digits of a given factorial n
def add_factorial(n)
a = (1..(n.zero? ? 1 : n)).inject(:*).to_s().split(//)
sum = 0
a.each do |x|
sum += x.to_i()
end
return sum
end
Written by Harrison Grieve
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#