Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 9 Online
OpenStudy (anonymous):

What am I doing wrong? ps1 I just don't get it! http://codepad.org/HRGKb3dE

OpenStudy (anonymous):

updated with some notes http://codepad.org/rbWmzJtK

OpenStudy (anonymous):

you can simplify line 30 to 'if 0 in results' you want your while loop (starting at line 27) to test all divisors between 2 and cand - 1 but you don't change div, it looks like that might be an infinite loop you want to run lines 27 thru 33 many times for many different candidates - you could do that by putting it all in a loop and changing cand at the bottom of the loop

OpenStudy (anonymous):

results.append(cand%div) If cand is only ever incremented by 2, and div is always held at 2, you will always end up with (cand%div) = 1, as the previous poster stated. The only thing I'd add is, to cut down on how many times you iterate through that loop, think about what conditions div has to meet to be worth using as a prime test. For instance, you wouldn't ever need div to be even. You also wouldn't need (div < cand) as the conditional in your while loop. If you've already tested 2 and 3 as potential divisors, then nothing greater than cand/3 could be a factor, right? I just used (my equivalent of) cand/2 as the exit condition for my loop. Can anyone tell me what the most computationally efficient way to code that loop would be?

OpenStudy (anonymous):

@OptimistIndustries http://en.wikipedia.org/wiki/Primality_test

OpenStudy (anonymous):

Premature optimization can lead to problems - get it working then if it is too slow try to figure out how to optimize it.-..... http://c2.com/cgi/wiki?PrematureOptimization http://www.theregister.co.uk/2007/06/22/premature_optimisation/ http://c2.com/cgi/wiki?RulesOfOptimization http://blog.jayteebee.org/2011/07/not-all-early-optimization-is-premature.html

OpenStudy (anonymous):

Hey thanks! You're the best!

OpenStudy (anonymous):

D'oh! didn't catch that div issue! here's an update with that fixed: http://codepad.org/QmSros70

OpenStudy (anonymous):

after the if clause at line thirty five your program is done - you have made a program that determines if the number nine is prime. to test more candidates you need to make lines 24 thru 38 repeat.

OpenStudy (anonymous):

because at the if clause at line 35, it's supposed to execute the yes_prime or not_prime sections, and then go again.

OpenStudy (anonymous):

wait i did it!!!! yay! i did it!! I consolidated everything and it started working! yay! i wrote a program! woot! http://codepad.org/Qp9j4qey

OpenStudy (anonymous):

cool. notice that you have duplicate instructions in both parts of your if/else clause - that means those instructions will always run and are unrelated to the if condition. when you see that, you should move the instructions outside the if clause - it shortens the number of lines and makes it clear that those instructions are not conditional: http://codepad.org/ITarGxda as you can see your program really only needs to know when 0 is not in results - so it can be simplified further: http://codepad.org/4jpaVWA6 hmm .. is 31 actually the 10th prime?

OpenStudy (anonymous):

I was able to re work it from there to get the correct answers to line up with the correct numbers. I was just so super excited that I got it to follow all the instructions! I'm still stoked about it. :D

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

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