Factorial of a number in python [duplicate] - python

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 2 years ago.
def fact(n):
if n<0:
return 0
if (n==0 or n==1):
return 1
else:
n * fact(n-1)
n = int(input("Enter a number"))
fact(n)
i dont know why its giving me a TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'

You failed to return a value from your recursion case:
else:
n * fact(n-1)
You need
else:
return n * fact(n-1)
The default return value is None, so you end up trying to multiply None by n at every level above the second from the bottom.
For instance, when you try fact(3), you recur ...
3 * fact(2)
2 * fact(1)
fact(1) is 1 ... return that ...
2 * 1 is 2 ... but you then return None ...
3 * None ... is illegal

I believe you meant
return n * fact(n-1)

Related

how do I solve? (recursive function question) [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 11 months ago.
I study recursive_function.
I think It have to print 120 ( 5 * * 4 * 3 * 2 * 1 )
but, It print 'None'
j = 1
def factorial(n):
global j
j = n * j
n = n -1
if n == 0:
return j
else:
factorial(n)
print(factorial(5))
You have to return when you make your recursive call. Rather than
factorial(n)
consider
return factorial(n)

How can I write a recursive function based on the outputs of a function python

I have to write a recursive function in python that generalizes the following function:
f(n) result
0 1
1 1
2 2
3 5
4 13
5 34
6 89
7 233
I made something like this:
def F(n):
if n <= 0:
return 1
else:
return 2*( F(n-1) + F(n-2) ) - F(n-3)
It works with almost all values of the range but it doesn't match with the 3 first results so.
How can I determine a recursive function based on the outputs?
I have this information:
"Hint: F(n) is recursively defined in terms of F(n-1) and F(n-2). You have to figure out the math expression that will make this happen.
Hint: no coefficient of the function in this expression is greater than 5, nor less than 1, and any such coefficient is an integer."
you need 3 base case
def F(n):
if n <= 1:
return 1
elif(n == 2): #-------->
return 2
else:
return 2*( F(n-1) + F(n-2) ) - F(n-3)
or
def F(n):
if n<= 1:
return 1
else:
return 3*F(n-1) - F(n-2)
A good source for integer sequences is OEIS. Your sequence has the number A001519. From there we learn that:
a(n) = 3*a(n-1) - a(n-2) for n >= 2, with a(0) = a(1) = 1.
Which is the solution #Epsi95 found.
Another nice property of this sequence is:
This is a bisection of the Fibonacci sequence A000045. a(n) =
F(2*n-1), with F(n) = A000045(n) and F(-1) = 1

Recursive Functions in Python vs C

It seems as if C allows for a function to reference itself (in a recursive manner) when executing, for example, the Collatz conjecture.
int collatz(int n);
int main(void)
{
int result = collatz(9);
printf("Steps: %i\n", result);
}
int collatz(int n)
{
if (n == 1)
return 0;
else if ((n % 2) == 0)
return 1 + collatz(n / 2);
else
return 1 + collatz(3 * n + 1);
}
Python gives me an error when I try this:
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
when I reference the same function within itself:
def collatz_recursive(n):
while n != 1:
if n % 2 == 0:
return 1 + collatz_recursive(n/2)
else:
return 1 + collatz_recursive(n * 3 + 1)
However, when I place collatz in its own function and reference it from the outside with collatz_recursive I don't have that problem:
def collatz(n):
while n != 1:
if n % 2 == 0:
n = n/2
else:
n = n * 3 + 1
def collatz_recursive(n):
while n != 1:
if n % 2 == 0:
return 1 + collatz(n/2)
else:
return 1 + collatz(n * 3 + 1)
The 'int' and 'NoneType' can not be added through + operation.
Here is my console for running your very last code:
line 13, in collatz_recursive
return 1 + collatz(n * 3 + 1)
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
I think the problem is that you forgot to handle the edge-case n=1 for your collatz_recursive function, you can easily log n by putting a print function before and after while loop and observe the bug.
Try to return a default value for your function, this is also named base-case.
You can read more about recursion and base case in here
"Python gives me an error when ... I reference the same function within itself"
No, Python gives you an error because collatz_recursive doesn't return a value for n == 1. The return value of a function is None if no value was returned with a return statement.
Your Python code should look like
def collatz_recursive(n):
if n == 1:
return 0
if n % 2 == 0:
return 1 + collatz_recursive(n/2)
else:
return 1 + collatz_recursive(n * 3 + 1)

Return keeps giving me None? [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 2 years ago.
Trying to build a recursive def that will counts how many times the number '7' occurs in a given number. I dont understand why my return keeps giving me None.
def count7(N):
'''
N: a non-negative integer
'''
x = 0
def helpcount7(N,x):
Ld = N % 10
NewN = N // 10
if Ld == 7:
x +=1
if NewN != 0:
helpcount7(NewN,x)
else:
print(x)
return helpcount7(N,x)
print(count7(717))
for example, if I input 717 my answer would be:
2
None
The reason is because you are not returning anything from your helpcount7() function. You should return something from it, for example:
def count7(N):
'''
N: a non-negative integer
'''
x = 0
def helpcount7(N,x):
Ld = N % 10
NewN = N // 10
if Ld == 7:
x +=1
if NewN != 0:
return helpcount7(NewN,x)
else:
return x
return helpcount7(N,x)
print(count7(717))

Output of the Python program is coming as 'None' [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 4 years ago.
def fact(n, summ):
if n == 0:
print(summ) -- Prints 55
return summ
fact(n-1, summ + n)
print(fact(10, 0)) -- Output None
You need to return fact(n-1, summ + n) as a returning value. If a function does not return a value then it defaults to returning None.
def fact(n, summ):
if n == 0:
return summ
return fact(n-1, summ + n)
print(fact(10, 0))
This outputs:
55
On a side note, your fact function could be re-implemented without the second parameter. The following produces the same output:
def fact(n):
if n == 0:
return 0
return n + fact(n-1)
print(fact(10))

Categories

Resources