recursive factorial function

此生再无相见时 提交于 2020-01-22 09:32:07

问题


how can I combine these two functions in to one recursive function to have this result:

factorial(6)
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720

these are the codes

def factorial( n ):
   if n <1:   # base case
       return 1
   else:
       return n * factorial( n - 1 )  # recursive call
def fact(n):
       for i in range(1, n+1 ):
               print "%2d! = %d" % ( i, factorial( i ) )

fact(6)
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720

as you see execution of these two gives a correct answer, I just want to make it to one recursive function.


回答1:


def factorial( n ):
   if n <1:   # base case
       return 1
   else:
       returnNumber = n * factorial( n - 1 )  # recursive call
       print(str(n) + '! = ' + str(returnNumber))
       return returnNumber



回答2:


2 lines of code:

def fac(n):
    return 1 if (n < 1) else n * fac(n-1)

Test it:

print fac(4)

Result:

24



回答3:


a short one:

def fac(n):
    if n == 0:
        return 1
    else:
        return n * fac(n-1)
print fac(0)



回答4:


def factorial(n):
    result = 1 if n <= 1 else n * factorial(n - 1)
    print '%d! = %d' % (n, result)
    return result



回答5:


try this:

def factorial( n ):
   if n <1:   # base case
       print "%2d! = %d" % (n, n)
       return 1
   else:
       temp = factorial( n - 1 )
       print "%2d! = %d" % (n, n*temp)
       return n * temp  # recursive call

One thing I noticed is that you are returning '1' for n<1, that means your function will return 1 even for negative numbers. You may want to fix that.




回答6:


I've no experience with Python, but something like this?

def factorial( n ):
   if n <1:   # base case
       return 1
   else:
       f = n * factorial( n - 1 )  # recursive call
       print "%2d! = %d" % ( n, f )
       return f



回答7:


fac = lambda x: 1 if x == 0 else x * fac(x - 1)



回答8:


Is this homework by any chance?

def traced_factorial(n):
  def factorial(n):
    if n <= 1:
      return 1
    return n * factorial(n - 1)
  for i in range(1, n + 1):
    print '%2d! = %d' %(i, factorial(i))

Give PEP227 a read for more details. The short of it is that Python lets you define functions within functions.




回答9:


One more

def fact(x):
    if x == 0:
        return 0
    elif x == 1:
        return 1
    else:
        return x * fact(x-1)

for x in range(0,10):
    print '%d! = %d' %(x, fact(x))



回答10:


I don't really know the factorial of negative numbers, but this will work with all n >= 0:

def factorial(n):
if n >= 0:
    if n == 1 or n==0:
        return 1
    else:
        n = n * factorial(n-1)
        return n
else:
    return 'error'



回答11:


Can use this 4 lines of code...

   def factorial(n):
        f = lambda n: n * f(n - 1) if n > 1 else 1
        for x in range(n):
            print('{}! = {}'.format(x + 1, factorial(x + 1)))



回答12:


And for the first time calculate the factorial using recursive and the while loop.

def factorial(n):
    while  n >= 1:
        return n * factorial(n - 1)
    return 1

Although the option that TrebledJ wrote in the comments about using if is better. Because while loop performs more operations (SETUP_LOOP, POP_BLOCK) than if. The function is slower.

def factorial(n):
    if  n >= 1:
        return n * factorial(n - 1)
    return 1

timeit -n 10000 -r 10

  • while 836 µs ± 11.8 µs per loop
  • if 787 µs ± 7.22 µs per loop



回答13:


There is always some kind of a loop in recursive functions and some stoping codes which stop the loop:

public int recursivefactorial(int number)
{
    if(number==1)
        return 1;
    else
        return recursivefactorial(number-1)*number;
}

As you can see the fulfilling the if condition leads to the code that actually ends the "loop" and this is the most important part of a recursive function. In contrast, the else part of the condition leads to calling recursivefactorial function once again which is effectively a kind of loop.




回答14:


One more =)

#FAC calculation

def fakulteta(x):
    if x!=1:
         return x*fakulteta(x-1)
    return 1


print (fakulteta(12))


来源:https://stackoverflow.com/questions/4502429/recursive-factorial-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!