Hi, im a begginer to python, and i've been doing it a small while now, i've been attempting some puzzles of a website, and have met a small problem, and i can't find it. My code is below and i will explain my problem, ask how to sort it, and ask if i am missing something important.

a = range(1,1000)
b = []
d = 0
for n in a:
    while n != 1:
        if n % 2 == 0:
            b.append(n)
            n = n/2           
        if n % 2 != 0:
            b.append(n)
            n = n*3 + 1
    else: b.append(n)
    if len(b) > d:
        d = len(b)
        b = []

Now the aspects of this code is to find out how long it takes to get to 1, using the 'Collatz Problem' i take the value of n to be inside the range, and if its not 1, i want it to go through the number, decide if its even or odd, then perform a calculation, firstl, im recording the number in a list, so i know the starting number, and then it will perform the calculation, i want it to continue this loop until it reaches 1, im not sure how to do this, i have the else statement at ehd end because i believe that is what i need, i then will take the length of b which is my list, and see if it is higher than my previous list, i then want it to record if so and erase the list, so i can start over at 0, with my next number, but i seem to not be able to make my list work, and i get memory failure of B several times, if someone can help, it would really be apprechiated.

Recommended Answers

All 8 Replies

So in summary (for lazy to read like me), what is the issue? Doesn't work as intended or throws exception? What is the error?

The error is, my b statement doesn't keep resetting which it should, and my list continues even once its reached 1, which i dont understand why :( also, im not sure if my else: statement is in the right place. Sorry i write a lot

Let's see what happens when n is 2. Your first if condition is true, you divide n by 2 and it becomes 1. Now your second if condition is also true! Welcome to the infinite loop.

should it loop, i didn't think it would, because thats while n != 1, so it should stop that loop and move onto the else: statement, or is that incorrect? i can't quite work out, how to make 2 loops, and when one ends, go to the next statement. That's the main problem im dealing with, sorry if im explaining wrong or being really useless

Shorten the list and add a temporary print for testing. See what that will do for you:

# shorten list for test
a = range(1, 10)
b = []
d = 0
for n in a:
    while n != 1:
        if n % 2 == 0:
            b.append(n)
            n = n/2
        elif n % 2 != 0:
            b.append(n)
            n = n*3 + 1
        else: b.append(n)
    print b, d, len(b)  # test
    if len(b) > d:
        d = len(b)
        b = []

At run time, things happen one step at a time. Let's trace the execution of your program with n being 2

while n != 1:
    # Python checks that n is not 1, and enters a loop
    if n % 2 == 0:
        # Python checks that n is even and enters the if-clause
        ...
        n = n/2 # [B]n becomes 1[/B]
    if n % 2 != 0:
        # Remember, n is 1, an odd number, so python enters the if-clause
        ...
        n = n*3 + 1 # [B]n becomes 4[/B]
    # while loop goes into next iteration, n is 4
while n != 1:
    # n is 4, enter the loop
    if n % 2 == 0:
        # n is even, enter the if-clause
        ...
        n = n/2 # n becomes 2
    if n % 2 != 0: # n is even, if-clause is not entered.
# Next iteration of while loop. n is 2. Etc, etc, etc

wow, that worked, thank you so much, if its not too much to ask, do you mind saying how you changed it, and what it does?, because i notice the elif: statement which im not good with, and one question about the last part, as i will only want one resut to show, will i need to change

#
print b, d, len(b) 
if len(b) > d:
d = len(b)
b = []

to something like

print b, d, len(b)
if len(b) > d:
d = len(b)
b = []
if len(b) < d:
b = []

and thank you very much

ok, i want to thank nezachem for showing why it went wrong, and i want to thankEne Uran as to showing me what was the way to fix it. I have finally sorted out my code, and i am very thankful to all of you for helping me. You have helped me understand and hopefully i can become better at python :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.