A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
I need a little assistance with pst1a. Here is a copy of my code.
http://dpaste.com/534798/
My thinking was, before count hits 1000, add 1 to x. If it is odd, do a for loop on it dividing x by every integer from 2 to x. If there is a remainder and x is not divided by itself then it must be prime, add to the list, add a count and break. So far its only giving me back all the odd numbers. Where did I go wrong in this logic?
anonymous
 5 years ago
I need a little assistance with pst1a. Here is a copy of my code. http://dpaste.com/534798/ My thinking was, before count hits 1000, add 1 to x. If it is odd, do a for loop on it dividing x by every integer from 2 to x. If there is a remainder and x is not divided by itself then it must be prime, add to the list, add a count and break. So far its only giving me back all the odd numbers. Where did I go wrong in this logic?

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Break only works within while/for loops http://docs.python.org/release/2.5.2/ref/break.html So what you are actually doing is for every odd number, creating a list starting at 2, all the way to x (line8) (@Line 9) Then using the first number in that list, 2, dividing your odd number by it and checking to see if X is equal to 2 (Which it isn't because you are only picking odd numbers) Obviously this will store every odd number because you are ALWAYS checking them against the integer 2. Try rethinking your usage of range in this function. =)

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0do the opposite case if there is not a remainder and they are not equal then break since it is not prime. so we move on to the next quicker. if you look for a remainder like now you are basically appending every situation that fulfills the if condition, but you just want to append the prime.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I also picked 2 for my range but one could just start at 3, won't matter, but you see I took the opposite case... # prime.py # finds primes up to 1000 list=[] for k in range(1001): if ((k%2)==1): for j in range(2,k+1): if( ((k%j)==0) and j<k): break elif (not(k%j) and j==k): list.append(k) print list

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I was able to get my code to work after I broke yours apart a little Beeb. count = 0 x = 1 prime = [2] while count < 1000: x += 1 for i in range(2, x+1): if x%i == 0 and x != i: break elif not(x%i) and i == x: prime.append(x) count += 1 print prime I just have a question. I dont understand what is happening in this line elif not(x%i) and i == x: Can you please explain. Thanks
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.