anonymous
  • anonymous
What is wrong with my problem set 1.1?
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
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");
anonymous
  • anonymous
EDIT: i made an error... in the line after while listlength <= max should be while d < i
anonymous
  • anonymous
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?

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
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.
anonymous
  • anonymous
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
anonymous
  • anonymous
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
anonymous
  • anonymous
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
anonymous
  • anonymous
haha. wow. you're right. It goes on forever. Let me see if i can fix the logic here. brb.
anonymous
  • anonymous
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.
anonymous
  • anonymous
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.
anonymous
  • anonymous
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.
anonymous
  • anonymous
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?
anonymous
  • anonymous
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.
anonymous
  • anonymous
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?
anonymous
  • anonymous
it prints d: 1 d: 2 d: 3 etc. all the way to d: 144093570820985 and beyond
anonymous
  • anonymous
or i take that back, not d: 1 becuase that is below it's start point
anonymous
  • anonymous
That's interesting. Can you paste EXACTLY what's in your module here?
anonymous
  • anonymous
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
anonymous
  • anonymous
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?
anonymous
  • anonymous
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?
anonymous
  • anonymous
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.
anonymous
  • anonymous
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!
anonymous
  • anonymous
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.
anonymous
  • anonymous
does the method i used seem fixable with a couple tweeks or should i redo the way it's structured?
anonymous
  • anonymous
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.
anonymous
  • anonymous
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.
anonymous
  • anonymous
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")
anonymous
  • anonymous
edit: i did not utilize the non_primes list if anyone happens to look at the source code

Looking for something else?

Not the answer you are looking for? Search for more explanations.