minor changes

This commit is contained in:
Vitor Oliveira 2021-04-02 11:22:53 -07:00 committed by GitHub
parent e90238e1f7
commit 30c9176098
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,18 +4,21 @@ Mathematical Explanation: The factorial of a number is the product of all the in
i.e: n! = n*(n-1)*(n-2)......*2*1 i.e: n! = n*(n-1)*(n-2)......*2*1
=end =end
#
# Approach: Interative
#
def factorial(n) def factorial(n)
if n < 0 return nil if n < 0
return nil
end
fac = 1 fac = 1
while n > 0 while n > 0
fac = fac * n fac *= n
n -= 1 n -= 1
end end
return fac fac
end end
puts '4! = ' + factorial(4).to_s puts '4! = ' + factorial(4).to_s