Merge pull request #58 from anubhav8421/add-solution-to-project-euler-problem-20

Add solution to project euler problem 20
This commit is contained in:
Anubhav Jain 2020-10-31 21:24:11 +05:30 committed by GitHub
commit 9755ecad5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,18 @@
# frozen_string_literal: true
# n! means n x (n - 1) x ... x 3 x 2 x 1
# For example, 10! = 10 x 9 x ... x 3 x 2 x 1 = 3628800,
# and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
#
# Find the sum of the digits in the number 100!
# method to calculate factorial of a number
def factorial(number)
number.downto(1).reduce(:*)
end
# fetch digits of factorial of `number` and find
# sum of all those digits, and prints the result on the console
number = 100
puts factorial(number).digits.sum