A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 4 years ago
Hey guys! I just started the course. I have no background in programming, but very ready to. I am on problem set one problem one. I have generated odd numbers > 1, but I'm not sure of the code or syntax to "then" find if the number is prime and how to terminate the code at the 1000th prime number. Please help!
anonymous
 4 years ago
Hey guys! I just started the course. I have no background in programming, but very ready to. I am on problem set one problem one. I have generated odd numbers > 1, but I'm not sure of the code or syntax to "then" find if the number is prime and how to terminate the code at the 1000th prime number. Please help!

This Question is Closed

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Hey Jswift  welcome welcome  thanks for the question. It'd be great to see the code you've written so far to help us help you out. http://codepad.org/ is a good site to use  make sure to click 'python' after you paste in your code. Hope this helps!

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0x = 0 for i in range(10): x = x + 1 if x % 2:

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0sorry the range should be 10,000. I was just testing the code trying to see what commands I needed to get to the next step. x = 0 for i in range(10000): x = x + 1 if x % 2:

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0I know that I need to divide each odd numbers by all numbers <= the square root of that number, but don't know how to represent that in the code. :/

maitre_kaio
 4 years ago
Best ResponseYou've already chosen the best response.0Try that: do as if you were explaining, in plain english, to someone a bit silly how to know if a number is prime. He should be able to follow your recipe. Post your explaination here.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0That's the key, yeah. I just finished figuring it out. Here's how I define the problem: A prime number is divisible by no other integer except 1 or itself. Check all numbers in sequence to see if they are only divisible by one, but not any of the prime numbers you have found so far. The way I kept track of the prime numbers I found so far is a data type called a list. Look it up. You can store prime numbers kind of like this : [2,3,5,7,11,13,17]. Also, you can use it with for loops to do iterations. I won't solve the whole problem, but the end result can include a loop that looks kind of like this : current = 1 primes = [] while len(primes) <10000: for divisor in primes: if current%divisor == 0: .....etc etc etc google the documentation for lists and try it out. it will make your code a lot easier to work with and much more readable if you use list comprehension to do it.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0mpeddle, Thanks for your response. I am not understanding where you got "for divisor in primes." Is "divisor" a primitive or one of the key words? Also with lists, I read the section you suggested, but I'm not clear on how I get information to propagate into the list. I assume that it goes with your "while" statement of "while len(primes)<1000:", but your program is very different from where I started so I'm having trouble incorporating the loop in my program. Please help.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Maitre_Kaio, Thank you for your response. I think where I am struggling is with the rules of the language and the explanation of the algorithm. I feel like I can say that I need to find all odd numbers > 1. Then, I need to divide each odd number, x, by all numbers > 1 up to x to see if there is a remainder. If there is a remainder then move to the next number. If there is no remainder then print the number. Mpeddle suggests using "lists" and I think I am understanding them more, but I'm not clear on how to get my answers into a list. In her sample of code she uses "while len(primes) < 10000:" I think this will terminate the code at the 10000th number, but I'm not sure. I'm just looking for a little more direction. Thank you both for your time and help!

maitre_kaio
 4 years ago
Best ResponseYou've already chosen the best response.0Ok I see you understand how to test if a number is a prime by using the trial division method. In you explanation, you even did a bit more work than I asked you. You did not just explain how to test if a number is a prime, but also how you would loop through the numbers to test all odd numbers. Now you should read that: http://www.greenteapress.com/thinkpython/thinkCSpy/html/index.html Chapters 18 are especially useful for you now, and you will understand how to translate your ideas into code. If you need more help, keep asking.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0Maitre Kaio has the right idea. You should read up on how to use a loop like a for...a little reading would go a long way. To answer your question, divisor is a 'throw away' variable that is instantiated by the for loop. The 'in' key word tell me that there are divisors inside of prime. Prime is a list object that contains prime numbers I have already found. For example, after searching for a bit it might look like [2,3,5,7,11,13]. When I use the for loop, it cycles through each of the numbers in my list of prime numbers and temporarily assigns that number to a variable called 'divisor' which I can use in the block of code run by the for loop.

anonymous
 4 years ago
Best ResponseYou've already chosen the best response.0i would look up the sieve of erastothenes (i think thats spelled right) to find prime numbers. basically, you eliminate all numbers divisible by 2, then 3, 4, 5, 6, and so on into the double digits.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.