5/15 Factorial


#1


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

Hi,

I don't seem to be getting the result I wish for and i cannot work out why the result is not a factorial.
What am i missing?

def factorial(x):
    result = 1
    if x > 0 and x == int:
        while x > 0:
            n = x - 1
            total = x * n
            x - 1
    return result

#2

For our purposes here we are safe to assume that x is a positive integer so do not need to validate it. Validating an integer is a story in itself that needs to be convered at some point in our learning, so do follow up with this.

def factorial(x):
    result = 1
    if x > 0:
        while x > 0:
            result *= x    # tweak on this line
            x -= 1         # tweak on this line
    return result

Say that x == 7.

1 * 7
7 * 6
42 * 5
210 * 4
840 * 3
2520 * 2
5040 * 1

#3

Thank you for your swift response.


#4

A post was split to a new topic: Got no idea why it works


#6

Here's what I did:

Had to go back a few lessons though to remember the import syntax :slightly_smiling:


#7

Did you at least attempt to do this without using the built-in module?


#8

I tried the method you described, I was close but not this concise and it was giving me an error stating to check for infinite loops.

But this made a lot of sense to set result to the initial number using x * 1, then subtracting and multiplying from there.

Thanks!


#10

you could use recursion...

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


#11

I got the syntax using the while loop :slight_smile:Thanks mtf
I just tried the import to have less line of code


#12

So long as you are confident you can do this procedurally, step by step. It stands to reason that we would use the built in functions whenever we can from the standpoint of performace. Python modules are compiled C (I believe), so are blazing fast, and there is less of our own code to compile at run time.


#13

:joy: it's the most efficient method.


#14

There r many methods, below is mine:


#15

Best answer yet..!!:joy:


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

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

#18

@aduku less lines

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

#19

Smart Answer!!:cactus:


#20

iteration:
def factorial(x):
if x <= 1:
return 1
counter = x-1
while counter > 0:
x= x * counter
counter -= 1
return x
recursion:
def factorial(x):
if x <= 1:
return 1
return x * factorial(x-1)
iteration takes less Program memory but recursion is easier to code


#21

hi mtf, i just started the codecademy course.
could you explain why you didnt use a while loop instead of the 'if x > 0'?

im new to this, and i wrote the code with 'while' instead of the 'if'. it still works but im just wondering if its bad code to write it as i have.
thanks


#22

That was based upon the opening post. Technically, the if x > 0 line is not needed since the while condition covers that.

f = 1
while x > 0:
    f *= x
    x -= 1
return f

It's not pretty but it always returns a number no matter what the input. 1 if x is 1 or less (even negative) else x!.

For our purposes we treat it as though the inputs are always valid. Error checking will come up later on.