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

Hey, I'm having trouble with problem set 1, question 1. The program runs so the problem is obviously in my semantics but I am not sure how to correct it. If some one could point me in the right direction, that would be greatly appreciated. Here's my attempt. Thanks x = 1000 initial = 10 pc = 4 if initial/1==initial and initial/2 != initial and initial/3 != initial and initial/5 != initial and initial/7 != initial: while pc <= x: initial = initial + 1 pc = pc + 1 print initial else: initial=initial +1

OpenStudy (anonymous):

okay. I ran this, and the first 2 numbers I got are 11 then 12. 12 in not a prime number. 12 can be divided by 2. But 12 divided by 2 is not 12 (initial/2!=initial)so the conditions are met for the if statement. To fix this, think about the use of '%' However, even if you fix the issue in the conditions, you will still have problems. What happens if a number is divided by only larger numbers than 7?

OpenStudy (anonymous):

also, try out dpaste.com It is much preferred than posting code direct.

OpenStudy (anonymous):

I don't think you can write this program given the tools you have at the start without creating some kind of loop to walk through.

OpenStudy (anonymous):

I agree, it would be better is you read more about iteration and loops, the for loop is actually very important in the first program. I would recommend reading through the python tutorial under the readings section. This is a program i modified from an example i found on there. PrimesCount=0 Number=2 while PrimesCount <1000: while (Number >= 2): for x in range(2, n): if n % x == 0: n=n+1 break else: # loop fell through without finding a factor print n, 'is a prime number' Primes = Primes + 1 n=n+1 break

OpenStudy (anonymous):

oh here is the dpaste link http://dpaste.com/581166/

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!