anonymous
  • anonymous
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
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
rsmith6559
  • rsmith6559
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.
anonymous
  • anonymous
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"
anonymous
  • anonymous
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

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
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.
anonymous
  • anonymous
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]
rsmith6559
  • rsmith6559
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.

Looking for something else?

Not the answer you are looking for? Search for more explanations.