mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
minor changes
This commit is contained in:
parent
e90238e1f7
commit
30c9176098
1 changed files with 14 additions and 11 deletions
|
@ -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
|
Loading…
Reference in a new issue