I'm working on ps1a and I cannot for the life of me figure out what is wrong here, can someone take a look? http://codepad.org/fW27x6wy
Take a look at this: http://codepad.org/DxHU4wI0 I added some extra print statement (and lowered the range!) so we can see what is going on. If I understand your code correctly, if statement on line 9 is supposed to determine if a number is prime. If the number is not prime, x increases, else if it is x and i increase. It looks like your test for primes is letting through some non-prime numbers (in this case: 9, 14, 15,). Look specifically at when it tests 9. 9 is not divisible by 5, so it think it is prime, however 9 is divisible by 3. You need to be testing x against all values from 2 through x (well, that isn't exactly true but I would start there). Try and rework your test for primes. If you need more help let me know. I am pretty new myself but I think I have a good grasp of this problem.
It might help to ignore the part of the problem where you have to look for multiple primes. Concentrate on the inner loop where you only have to see if a single number is prime. Once you get that completely working, then add the outer loop back in.
I struggled with this problem for a few hours. I know it feels like they throw you off the deep end, don't give up! The biggest thing that helped me (after banging my head against the wall for a while) is that I started by breaking down the problem into much smaller chunks. Try to solve the smaller problems first (like how do I write a piece of code to check if a number is prime). Once I have that, how do I print the first 10 primes. Working in smaller chunks helps your brain keep things manageable. Good luck! You'll get it :)
The first specific reason your code isn't working can be seen in seanlerner's code's output. You're only testing one divisor (m) per number (x).
Thanks for all the help. I was working on this problem for like 6 hours straight yesterday and couldn't get it working. I realize that the value I have for m is not trying all the values in the range(2, x), but I'm not sure why. When I run x % m I want m to check everything (like if x =5, it should be 5 % 2, 5 % 3, 5 % 4, but it doesn't seem to be working that way.
Seanlearner, thanks for the updated code. I'm checking m in it but I can't seem to understand how m is changing (it's obviously not doing what I want it to). M is changing at like 2, 2, 3, 2, 3, 4, 5 for each increment of x, and it's not checking each value in the range either.
I would start over and approach the smallest part of the problem, which is testing if a number is prime. Try writing a function that does that (as andreipop recommended). Start by defining a function, (for example, isPrime(n):) Then make sure n != 0 or 1 (we know these are not primes) Then use a for loop to test i in range(2, n) to see if n is prime (like line 9 in your code...) Then either return false or true. If you put that together correctly, you should be able to call the function and test if any integer is prime. Once you figure that out and have it working, then you can move on to finding the 1000th prime (it will be a lot easier to do if you have a prime test function to call). Hope that helps!
I think part of the problem with PS1 is that we're not yet supposed to know how to write functions, so we can't break up the tasks into clean sub parts. The solution is supposed to be nested loops. But it seems like all the questions on this site about PS1 are from people who are trying to write both loops at once. If that ever works, it's luck. Especially for a beginner programmer. I'm a professional programmer of 13 years and I started by writing the inner loop by itself.
See the attached solution. Let me know if you like it. If you need more help, let me know. Unfortunately, I am not all the day available. All the best
@pionut Yes, that is a good solution to the problem. But you haven't helped SunRises figure out where his thinking went wrong, or helped him learn how to write a program. Understanding code and writing code are two very different skills. The point of this problem set (and all programming problem sets, really) isn't to generate prime numbers, but to practice writing a program. Now he will probably look at your answer and say, "I understand that. I probably could have come up with that myself." But he will have missed out on the learning experience of working through it, and figuring out what works and what doesn't, which is an important step in learning how to program.
Join our real-time social learning platform and learn together with your friends!