Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
rwoolfson
Group Title
Intro to CS  Prime Number Problem help
I'm auditing the online intro to CS in Python (MIT 6.00) and am strugling with the problem set number 1 on computing the 1000th prime number. I don't feel that I'm very close. I'm open to suggestions. Here's my code thus far...
prime_count = 1
primes = (2, )
prime_test = 3
while prime_count <=1000:
if prime_test%2 == 1:
prime_test += 2
for n in range(3,prime_test/2):
if prime_test%n > 0:
prime_count += 1
print primes + (prime_test,)
The output I get is nothing near prime number. It is
 11 months ago
 11 months ago
rwoolfson Group Title
Intro to CS  Prime Number Problem help I'm auditing the online intro to CS in Python (MIT 6.00) and am strugling with the problem set number 1 on computing the 1000th prime number. I don't feel that I'm very close. I'm open to suggestions. Here's my code thus far... prime_count = 1 primes = (2, ) prime_test = 3 while prime_count <=1000: if prime_test%2 == 1: prime_test += 2 for n in range(3,prime_test/2): if prime_test%n > 0: prime_count += 1 print primes + (prime_test,) The output I get is nothing near prime number. It is
 11 months ago
 11 months ago

This Question is Open

rsmith6559 Group TitleBest ResponseYou've already chosen the best response.0
Your not that far off. IMO, the most important skill that a new programmer needs to master is decomposing (breaking down) problems. Break things down into small, just a couple/few lines of code. Separate the sections of code with a blank line, so they're sectioned visually, and the blank lines also suggest where comments should probably be. primes = (2, ) Why use a tuple? Tuples are immutable (they can't be changed). A list is better suited if you're going to append each prime. You want to think about where you're incrementing prime_test. You may want to think about what you're testing to see if prime_test is prime.
 11 months ago

rwoolfson Group TitleBest ResponseYou've already chosen the best response.0
Thanks for advice. I bought the suggested textbook and plan to go through concepts in more detail. I believe I successfully wrote code for finding the highest odd number from a group of 3. Seems kind of long for a simple task, but its the best I could come up with. Any thoughts... x = 1289 y = 91 z = 203 if x%2 != 0: if x > y and x > z: print x,"is greatest odd number" elif y%2 == 0 and z%2 == 0: print x,"is greatest odd number" elif x > y and z%2 == 0: print x,"is greatest odd number" elif x > z and y%2 == 0: print x,"is greatest odd number" if y%2 != 0: if y > x and y > z: print y,"is greatest odd number" elif x%2 == 0 and z%2 == 0: print y,"is greatest odd number" elif y > x and z%2 == 0: print y,"is greatest odd number" elif y > z and x%2 == 0: print y,"is greatest odd number" if z%2 != 0: if z > x and z > y: print z,"is greatest odd number" elif x%2 == 0 and y%2 == 0: print z,"is greatest odd number" elif z > x and y%2 == 0: print z,"is greatest odd number" elif z > y and x%2 == 0: print z,"is greatest odd number" if x%2 == 0 and y%2 == 0 and z%2 == 0: print "no odd numbers"
 11 months ago

ronin12 Group TitleBest ResponseYou've already chosen the best response.0
i think a shorter way to write that code would be to put your 3 numbers into a list. then parse the list (go through each element of the list) and remove any even numbers. then its just a matter of sorting the list and returning the largest one
 11 months ago

rwoolfson Group TitleBest ResponseYou've already chosen the best response.0
Thanks. I haven't learned how to make a list of numbers or remove a number from a list. I would appreciate the specific code.
 11 months ago

ronin12 Group TitleBest ResponseYou've already chosen the best response.0
a list is created the same way you make a tuple except you use [ and ] instead of ( and ). so for example you would have list = [1298,91,203] you can then have a command like list.remove(91) and it would remove the value 91. then list.sort() will put them in order. finally list[1] will select the last item in the list. x = 45 y=6 z=51 listOfNumbers = [x,y,z] for n in listOfNumbers: if n%2 == 0: listOfNumbers.remove(n) listOfNumbers.sort() print 'the largest odd number is:',listOfNumbers[1]
 11 months ago

rsmith6559 Group TitleBest ResponseYou've already chosen the best response.0
I like the styling in this code much better. Writing code that you can come back to years later to make a change to is an art form. I have code that I wrote in 1997 that's still in use. I didn't use descriptive variable names, my commenting was sparse at best, and I dread having to go in and make changes to it. In addition to your textbook, I'd recommend going to http://docs.python.org and find your version. The tutorial will help you get going with things like lists and tuples. If you use a list, I'd first check if any of the numbers are even and eliminate them. Then compare which of two of the numbers is larger, remembering the larger one and comparing that to the next one and so on until all of the numbers have been checked. If your textbook has talked about functions, that would be the way to go.
 11 months ago
See more questions >>>
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.