Factorial


#1

I'm not really sure how to approach this problem. So far I've got:

I understand that it wants me to mutiply x by x-1 then x-2 then x-3 etc until it gets to 0 but how do you phrase that in code?


#2

declare a variable called total, give it a value of 1. (Given multiplying x by one is always x)

then you can loop over the numbers and multiply them by total


#3

I'm sorry but I don't understand. I feel so stupid lol because this is probably something very obvious and simple. What exactly do I return after the for statement?

def factorial(x):
total = 1
if x == 0:
return total
for num in range(1, x+1):
return


#4

wait.. the x == 0 part is so wrong.. ignore that for a second, I'm still confused about the question


#5

you could use this for base case if you want a recursive solution (which i would advise against)

so now that we have total, we need to loop over all the numbers in range, and multiply (*) them with total. Make sure you update total in the process


#6

I don't know what's up with me and this exercise.

Does total refer to x * (x-1) ? Could you give me the answer so I can look over it or at least part of it so I can see what I'm aiming for?


#7

I'm looking over the loop section again and I understand everything but when it comes to this problem I don't know what I'm supposed to apply to it


#8

total refers to total:

total = 1

then your loop is good:

for num in range(1, x+1):

so now we just need to multiply all this numbers with total:

total = total * x

Or did you want the recursive solution?


#9

I've already tried this but it returned an error.
I'll try again:

Oops, try again. factorial(3) returned 3 instead of 6


#10

Yeah, I was aiming for the recursive solution originally but I didn't know how to phrase it


#11

tell me, what solution do you want to go for:

the loop where you calculate total, and then after the loop return total

or recursive solution,

okay, i was aiming for the loop solution given its easier. Which one do you want? I would recommend the loop


#12

Ok, the loop then since I've obviously got a long way to go to understand the recursion properly


#13

good choice :slight_smile:

for the loop, we need to multiply all numbers in range with total:

total = total * x

then after the loop, return total


#14

but if we set total = 1 at the beginning doesn't total * x then equal to x?


#15

oops, that should be total * num, to get all numbers from list generated by range.


#16

like this? but it still returns an error that factorial(2) returned 1 instead of 2


#17

a function ends the moment a return keyword is reached, so your loop currently breaks in its first iteration.


#18

Ok...
I've got to step out for a bit atm because I'm late for something but I'll be back later.

Thank you for the help so far, I'll post back when I get back


#19

Ok so after looking over the problem further:

the return keyword should not be further indented because it breaks the loop after the first iteration in that case..

However, I'm still confused about for num in range(1, x+1)

lets say we start off at x=2 in which case the total is 2*1 = 2, then we go on to x+1 = 3 and the total is 3*2*1 = 6 but below the for loop we wrote that total = total * num which would lead me to think it would simply yield 1*3 =3. Is there some sort of implied meaning to total = total * num? What am I missing?


#20

range does not includes it stop value, so if you have x=2 you get:

def factorial(x):
   total = 1
   for num in range(1, x+1):
       print num, total
       total = total * num
   return total

print factorial(2)

i added some print statements, for x=2 you have:

1 = 1 * 1
1 = 1 * 2

this is the math done by the loop for x=2