5. Factorial Infinte Loop


#1




Link


It appears to just be an infinite loop. Obviously I expect this to find the factorial of any positive number, x. If you could break this down and tell me what's wrong with it, that would be most appreciated. Cheers


def factorial(x):
    myList = [1]
    while x > 0:
        myList.append(x)
        x -= x
        #for loop works perfectly
    increment = 0
    value = 1
    while increment < len(myList):
        value *= myList[increment]
        increment += increment
    return value


#2

I don't know why it is an infinite loop but in your first while statement the output seems to be "myList = [1, x]".
So even if everything goes perfectly your second while will only return "x" no matter what x is.
if you want to use while come from the reverse like x! = x* (x-1) * (x-2) .... 2 * 1
keep decreasing x


#3

But it shouldn't only return x, it should return value which is the product of all elements of the array multiplied together. Algorithmically, I believe it should work, and I can't find anything syntactically wrong.


#4

def factorial(x):
myList = [1]
while x > 0:
myList.append(x)
x -= 1
#for loop works perfectly
increment = 0
value = 1
while increment < len(myList):
value *= myList[increment]
increment += 1
return value

This does the trick.


#5

I don't understand. why the mylist?


#6

If you are referring to myList = [1], that is because of the nature of factorials. That is, wether you enter 4! or 5! etc, the one constant within the list will always be 1. The rest of the first while loop related to the my.List is simply to create a list of all the numbers for the factorial computation. So 4! becomes the following list:
myList = [1,4,3,2,]
I hope that helps


#7

That is true dommo, but my code still fails, and I still don't understand how it was an infinite loop. If the function takes an input 2, the array will be [1,2,1], so len(myList) should be 3. The comparator is "len(myList) < increment" works whereas "len(myList) > increment"(where increment = 0 initially) results in an infinite loop. This seems totally backward because 3 > 0 should evaluate to true but instead it's 3 < 0.


#8

I used the range function. It seamed more simple.
range(x,1,-1) ---> where the x is the start, stops at 1 with steps of -1.
For example if x is 5 then the range function return [5, 4, 3, 2]. Neat!

def factorial(x):
    result = 1
    for i in range(x,1,-1):
        result = result * (i)
    return result

x = 5
print factorial(x)

#9

I did not realize you could do that with the range function. I was trying to use a while loop similar to the original poster. I was thinking I may have to use two loops and then I was like this should not be this complex to need 2 loops to process the data.

It makes much more sense to see it this way. I guess I need to study up on that range function a little more, the previous lessons only briefly touched on it. Seems like a powerful function which will take me some time to learn.

Thanks again.


#10

Oooo...Now i see the issue. remember while loops run/iterate as long as the condition is true. As you stated, 3 will always be greater than zero. so it will run infinitely. To solve this you just need to change 'increment += increment' ---> 'increment += 1' and 'x -= x' ---> 'x -= 1'


#11

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