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
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?
also, try out dpaste.com It is much preferred than posting code direct.
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.
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
oh here is the dpaste link http://dpaste.com/581166/
Join our real-time social learning platform and learn together with your friends!