mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-14 08:01:05 +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
|
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
|
Loading…
Reference in a new issue