Python :15.5 Practice Makes Perfect(Factorial)


#1

Hello,Can anybody look at my code

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/5?curriculum_id=4f89dab3d788890003000096#

I get following error message

Oops, try again. factorial(4) returned 12 instead of 24


def factorial(x):
    if x==1:
        return 1
    else:
        return (x)*((x)-1)
        

print factorial(4)


#2

The apparent intended method was a recursive function, which, however, this is not. As factorial goes, a recursion looks elegant but is in fact not the approach to reach for. I'm sure the jury is out on that one. Iteration is the way to go. Work to develop an iterative function that returns a factorial.

The recursion you were after looks like this (to which you were very close)

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

>>> factorial(7)
5040
>>>

27! is a huge number...

>>> factorial(27)
10888869450418352160768000000
>>>

That's 28 magnitudes of 10, 5 magnitudes greater than the distance light has travelled since the start of the of known universe.

>>> 1.38e10 * 1e13
1.38e+23            # distance in kilometers of 13.8 billion light years
>>>

For the record, even Python does not use recursion in the math.factorial function.

for (i=1 ; i<=x ; i++) {    // C

On this page, F3 math_factorial.


#3

If you go by your function definition, print factorial(4) prints 12, instead of 24. I’ll tell you why.

else:
    return (x)*((x)-1)

What that is doing is multiply just x by (x - 1) i.e., 4 * 3 and return the product.

What you would want it to do is return the product of 4 * 3 * 2 * 1.

To achieve that, use either a loop or recursion.

By the way, 0! is 1, too.


#4

Thanks For your inputs. I solved the problem using recursion method. But I couldn't solve it by looping. As far as I know we need to to loop using range(x) method. But then I got lost.Can you provide Hint?


#5

OK. Initialize a result variable to 1.

If the function parameter x is 0, return 1.

Else, go for a range of 1 to x + 1, multiplying the result by each number in the range and iteratively assigning the product to the result.

Let me know if you still can't get it.


#6

This is my code:

def factorial(x):
    fact=1## why assign to 1 instead of zero??
    if x==0:
        return 1
    else:
        for i in range (1,x+1):
            fact=fact*i
        return fact
            
            
print factorial(4)

Thank you very much for your time and counsel


#7

Glad I could help :slight_smile:

Assign zero and see what it does.


#8

Oh now I understand ! Assigning fact to zero will render everything zero


#9

If you are not busy could you care to take a look at my code for https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#

def is_prime(x):
    if x>2:
        for n in range(2,x-1):
            if x%n==0:
                return False
            else:
                return True
    elif x==2:
        return True
    else:
        return False
        
        
print is_prime(2)
print is_prime(3)
print is_prime(4)

I get following output

True
None
False
None

I am sorry if I am annoying you or wasting your time or disturbing your sleep If you are in Asia


#10

Change if x>2: to if x > 1:

Then, loop range should be (2, int(x**0.5)+1). Hope you understand why I'm taking the root of x and converting it to an int.
* If x is divisible by n with a remainder of zero, x is not prime.
* Else, x is prime.


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.