Factorial (helplessly lost)


#1



Here's the exercise.

I am so frickin lost. I understand what it's trying to get me to do. I just have absolutely no clue how to execute it. I've been trying forever and I'm about ready to quit tbh. Is there anyone who can walk me through it without just giving me the answer? I feel like I keep going in circles and I just need a guiding hand. We can start at the beginning cause nothing I've done seems to work


#2

Of course, as I'm in the codecademy labs it gives me the flipping answer :rage:

But I really want to understand how/why it works. I'm so confused.


#3

We know a factorial multiplies from high to low, going down 1 each time. Unfortunately, a for-loop goes from low to high. So how would you go from high to low?


#4

Ummm, rearrange the list?


#5

Or incorporate n-1 some how but I don't know how


#6

and wouldn't multiplying low to high come up with the same answer anyway?

1*2*3*4*5 == 5*4*3*2*1

right?


#7

A list would over-complicate things a bit.

Although, you're right xD I guess I over-complicated mine a bit.

I had used a while loop, but a for-loop can work


#8

All that matters is getting from point a to b while multiplying everything in-between and storing that data to then be returned.


#9

In the hints it said to consider calling factorial(n) on itself?

How can you do that if you haven't defined the function yet?


#10

Also what is a recursive function? I was looking over the forums earlier and it mentioned them but I don't think we've learned about those in the course?


#11

That's recursion. Recursion is like a more advanced loop that can be very useful in certain cases. I can see it working here too. As long as you have a break case, (in the hint it's 1) it will call itself over and over until the break case is reached. If you had function(x) you can call function(x-1) inside of that which will call that function(x-1) before moving on to the remaining code inside of function(x). The remaining code will be executed once a break case is reached and the computer will start working in reverse (First In, Last Out). They haven't taught them yet, which is odd that it would be in the hint.

This article might be helpful: Recursion


#12

Okay so there was a lot of stuff in that article that was way over my head. However I did come up with this:

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

Which is infuriatingly more simple than the crap I was trying to do before. And it works so woo! But I don't quite understand why. I don't get it how it's iterating over every number in a range (1-n) without a loop?

Like if I called factorial(5) wouldn't it just give return 5 * (5-1)? When I run this it gives me the right answer of 120.


#13

oh wait wait I get it.

if you were to write it out it would look like

5 * factorial(5-1)

5 * factorial(4)

5 * (4 * factorial(4-1))

5 * (4 * factorial(3))

5 * (4 * (3 * factorial(3-1)))

5 * (4 * (3 * factorial(2)))

5 * (4 * (3 * (2 * factorial(2-1))))

5 * (4 * (3 * (2 * factorial(1))))

5 * (4 * (3 * (2 * (1)))) <<---- since factorial(1) returns 1

Right?

Jesus that's hard to wrap my head around but I get it now.

Thank you!! I didn't realize you could call the function within itself.


#14

Well, I just scrapped my post xD
Yep, you got it!
Whenever I use recursion, I like to keep a pen and paper handy to keep track of it.


#15

What was your solution with a while loop? Just curious.


#16

def factorial(x):
    total = 1
    while x > 0:
        total *= x
        x -= 1
    return total

#17

That's super interesting! I tried something similar when I first started but it threw an error at me when I tried to use *= saying it didn't recognize it or it was a syntax error or something.


#18

You could also do:

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

hmmm... maybe Codecademy just wanted a different way? Not sure. Either way, I can ensure it does work, at least in Python 2.7


#19

That looks very similar to what I tried and it didn't like it. Oh well. Thanks so much for your help!


#20

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