From 30c9176098283561bee7598eaaebe3019bd345cd Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Fri, 2 Apr 2021 11:22:53 -0700 Subject: [PATCH] minor changes --- .../{factorial_iterative.rb => factorial.rb} | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) rename maths/{factorial_iterative.rb => factorial.rb} (72%) diff --git a/maths/factorial_iterative.rb b/maths/factorial.rb similarity index 72% rename from maths/factorial_iterative.rb rename to maths/factorial.rb index cc070b3..e83b347 100644 --- a/maths/factorial_iterative.rb +++ b/maths/factorial.rb @@ -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