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
=end
#
# Approach: Interative
#
def factorial(n)
if n < 0
return nil
end
fac = 1
while n > 0
fac = fac * n
n -= 1
end
return fac
return nil if n < 0
fac = 1
while n > 0
fac *= n
n -= 1
end
fac
end
puts '4! = ' + factorial(4).to_s