What is wrong with my problem set 1.1?

i = 3 max = 1000; nonprimes = [] primes = [2] d = 2 listlength = len(primes) while listlength <= max: if i%d != 0: if d + 1 == i: primes.append(i) elif d + 1 != 0: d = d+1 elif i%d == 0: nonprimes.append(i) i = i + 1 print primes raw_input("press any key");

EDIT: i made an error... in the line after while listlength <= max should be while d < i

Couple of things: The indentation as it's typed here is weird. the line after while listlength <= max: should be indented ONE tab-length further than the line above it, not two as seems to be the case here. Either something's funny about the way it's entered here or that middle section needs to be dedented. If it runs, it's probably some sort of error in the copy/paste. len is a function. That means that the command listlength = len(primes) calls the function len, which returns an integer. In this case, len(primes) = 1, so listlength = the integer 1. That's important, because it means that the value of listlength DOES NOT change as the list primes gets longer. If the length of the list primes changes and you want to reset listlength to the new value, you need to do that explicitly, with another listlength = len(primes) statement. In this code, listlength will never change after it's assigned. also, your test of primeness is hard for me to understand. How are you trying to check each number?

yeah... i'm checking each integer for if it is divisible by every integer below it. if this is the case, append to primes. if not, append to non primes. also, how do i get it to check if it has changed in the while statement. I'm confused as to how i could give a check to see whether list length is still less than 1000.

also, the reason for the misindentation is because i was missing a line after while listlength < 1000 it is supposed to read: while d <= i: and then continue with the if statements

Here is the updated code, i hopefully avoided the listlength problem all together by grabbling the length directly as opposed to through a variable. I hope that is semantically correct. i = 3 max = 1000; nonprimes = [] primes = [2] d = 2 while len(primes) <= max: while d <= i: if i%d != 0: if d + 1 == i: primes.append(i) elif d + 1 != 0: d = d+1 elif i%d == 0: nonprimes.append(i) i = i + 1 print primes raw_input("press any key"); it still does not run

One of the most useful things to know when you're trying to write code is what exactly "does not run" means. If the program doesn't terminate, it's probably in an infinite loop, which means you should check the variables controlling your loops. If you get a syntax error, it will highlight the line that's (probably) causing the problem. If you get a traceback error message, it will include the line that's causing the problem and the specific problem. Understanding how to solve each of those problems isn't ALL you need to know, but a lot of the time it's the difference between looking for the correct solution and grasping at straws. In this case, when I run the code it doesn't terminate, so I know the problem must be one of the variables tested in the loops. To figure out which one it is, we can put in print statements. Try running this code and seeing what happens: i = 3 max = 1000; nonprimes = [] primes = [2] d = 2 while len(primes) <= max: print 'len primes: ', len(primes) while d <= i: print 'd: ', d if i%d != 0: if d + 1 == i: primes.append(i) elif d + 1 != 0: d = d+1 elif i%d == 0: nonprimes.append(i) i = i + 1 print primes

haha. wow. you're right. It goes on forever. Let me see if i can fix the logic here. brb.

i cannot wrap my head around why d would continue to grow until infinity because it is limited by the length of i. the length of i is 3 to start with.

First, notice that d ISN'T growing. It starts out as 2, i starts out as 3, then the program evaluates: if 3/2 leaves a nonzero remainder: if 2 +1 equals 3: add 3 to the list of primes repeat.

if you insert a statement printing the list of primes, I think you'll find that 3 keeps getting added over and over and over.

When the problem is that a variable isn't changing, you have to figure out why. Where is the step that's supposed to add something to d, and why isn't it working the way it should?

i see what you're saying now, and that's what i would think would happen, but... d continues to grow when i execute the program as demonstrated through your print statements.

When I run this program, the value of d is the integer 2, and it prints out over and over and over. What do you mean that d continues to grow?

it prints d: 1 d: 2 d: 3 etc. all the way to d: 144093570820985 and beyond

or i take that back, not d: 1 becuase that is below it's start point

That's interesting. Can you paste EXACTLY what's in your module here?

i = 3 max = 1000; nonprimes = [] primes = [2] d = 2 while len(primes) <= max: print 'len primes: ', len(primes) while d <= i: print 'd: ', d if i%d != 0: if d + 1 == i: primes.append(i) elif d + 1 != i: d = d+1 elif i%d == 0: nonprimes.append(i) i = i + 1 print primes

Oh, i see. in your version, the line elif d + 1 != 0: is elif d + 1 != i: Also, in your version the i = i+1 is dedented one time, but I think i indented it by mistake. So now I see the same thing as you. There's still a problem with the loop, but d is growing. So what's happening to i?

don't i want it to be indented just one time because i want it to run through all the division and then go to the next i?

Sorry, I've got to go for the rest of tonight. I'll look at it again tomorrow. If you keep working and have more questions, feel free to leave them.

yeah it's no problem man. i think it's awesome that you take the time out to help. i'll probably change the way i'm doing it a little and when i do i'll repost it! thanks so much for the pointers it was a great help!

You are stuck in the 2nd IF statement. You basically coded: if 3 = 3 then append primes with i. Well, d + 1 = 3 and you will do this forever.

does the method i used seem fixable with a couple tweeks or should i redo the way it's structured?

To be honest I didn't look it over in it's entirety. I'll take a look and get back when I get a chance.

You're going to have to fix the way the first and second while loops work together, and that probably means changing a relatively large part of your approach. Right now, both d and i grow (or fail to grow) within the inner while d <= i: loop, so the outer loop never gets a chance to check whether len(primes) <= max The way to solve that problem is to describe how those loops are meant to work together and rewrite them so they do. I think the best way to do that might be to start again from a step-by-step description of what you want the program to do--not because you don't have useful things here, but because the impulse to tweak without a broader understanding of the program usually leads to lots of frustration and wasted time, and almost never leads to a good solution or a clear understanding of what's going on.

well, i got a chance to look at it again today and i am relieved to say i got it to at least print out the primes. I was a little frustrated, but the way i am approaching things when i debug is to enter in values and do the operations the computer would do line by line, much like a math problem. when i used that approach, i litterally debugged the logic flaws in about 15 min. Thanks somnamniac! your tips helped! heres the code: non_primes = [] prime_numbers = [2] x=3 d=2 while len(prime_numbers) <= 1000: print len(prime_numbers) while d < x: test = x%d if test != 0: if d + 1 == x: prime_numbers.append(x) d = d+1 elif test == 0: d = x x = x+1 d = 2 print prime_numbers raw_input("please press any key to exit")

edit: i did not utilize the non_primes list if anyone happens to look at the source code

Join our real-time social learning platform and learn together with your friends!