Hello. I could use help with Problem Set 1: Problem 1. "Write a program that computes and prints the 1000th prime number." I am following the hints but get stuck at generating all odd integers >1 as a candidate to be prime. I know how to determine if a number is odd or even but am not sure how to successfully move through the numbers and also save them. I'm looking for additional hints, not answers. Many thanks!
looks to me like you'd have to use a loop...
first of all U can generate any integer and check if it even or odd. If it is even u throw it away. Next, U need to check if it has more than 1 factor then if its true u throw it awa again.. and proceed until u get ur answer. U can post ur code snippet here for folks to correct. Which language are u using?
You could save the primes in a list. i = 3 while( some condition): # do something i +=2
@rsmith6559.... is this code for prime number?
Yes.
Kindly tell its working. I'm confused not getting your point how it prints prime number?
This is the problem where you need to generate a code that tells you the 1000th prime number. The problem states we are to write it in Python. Thanks to all who have responded. I was sure I'd need to use a loop and also that I will need to generate integers and only keep the odd ones and, of those, only the ones that are prime. I'm just not sure where/how to "store" them once I've done that. What I mean is, I know the code for determining if a number is even or odd. And I know the code for determining if the number is prime or not. (I think) But at the moment I can only do this for one number at a time. I think I'm missing the part where I can hold onto these numbers and then use them later. I hope I made sense. Thank you again for your feedback. More is most welcome!
rsmith6559: I see you showed saving primes in a list but I don't understand where that saves them or, more importantly, how I can retrieve them. Thanks.
A list is a collection of (usually the same kind of) objects. Don't let the definition throw you. primesSoFar = [ 2, 3, 5, 7, 11 ] len( primesSoFar ) # 5 primesSoFar.append( 13 )
Hmmm. Thanks for the response but you've gotten a bit ahead of me. Let me soak in what you all have said so far and I'll see what I come up with. Thanks to everyone for the help! This is a new way of thinking for me and I need to take some time. Susan
Probably one of the best things that you can learn is to break the problem down into smaller problems, break them down and so on until you have manageable problems to solve.
Hey Susan - I am following the class, too. The difficult thing about the other responses is they suggest things we haven't learned in the lectures (only assigned readings.) I resolved the solution by creating a counter variable instead of tracking the length of a list. Each prime number I reached, I added 1 to the counter. Also, I saved each calculated prime number into a variable last_prime. Since the problem requires only the 1000th prime, not all the others, I used a while loop... while counter > 1000. If you want to see a copy of my code, please just let me know. I can post here, or email. I'm sure others here could point out better code or inefficiency, but I used only the things we've covered in class.
Join our real-time social learning platform and learn together with your friends!