Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

SgtRock

  • one year ago

I am STUMPED!!! I have read through my code and I can't see why it is missing some numbers as even and calling them primes. If any good samaritans out there could review this I would be appreciative. print "Thank you for using Greg's Prime Number Generator" ## primes = raw_input ('How many Prime Numbers do you need?: ') primes = 20 count = 0 n = 2 while count < primes: if n < 3: ## This denotes 2 as a prime count = count + 1 print count, n n = n + 1 else: hid = int (n**(0.5)) + 1 # Resets var (hid) to sqrt of n (+ 1)

  • This Question is Open
  1. SgtRock
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I got cut off... print "Thank you for using Greg's Prime Number Generator" ## primes = raw_input ('How many Prime Numbers do you need?: ') primes = 20 count = 0 n = 2 while count < primes: if n < 3: ## This denotes 2 as a prime count = count + 1 print count, n n = n + 1 else: hid = int (n**(0.5)) + 1 # Resets var (hid) to sqrt of n (+ 1) if (n >=3) and (n % 2) == 0: print "num:", n,'Not Prime Even' n = n + 1 else: while hid > 1: if n % hid != 0: print "num:", n, "trying denominator:", hid hid = hid - 1 else: print "num:", n, 'Not Prime Odd' n = n + 1 hid = 0 else: count = count + 1 print count, n n = n + 1 else: print 'End of List'

  2. Ante
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    There's a minor mistake in the second while loop: after finding an odd number it always takes two steps instead of one (n is incremented both inside and outside the loopl). I'm still thinking about an elegant solution, though, but I guess you can take it from here ;)

  3. bwCA
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    please use a code pasting site: - http://dpaste.com - http://pastebin.com - http://www.repl.it/ - http://pastie.org - http://codepad.org - http://ideone.com paste your code there and post the link here. select Python syntax highlighting when u paste.

  4. bwCA
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    maybe try writing out the steps you need to take to accomplish the task first - then turn that 'pseudocode' into code; revising the ''pseudocode' as you progress.

  5. michaelokt
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    As the professor said in class, most of our time as programmers is spent debugging. So, I've found debugging skills very useful. They're prevented me from breaking stuff in my room many times. You do have print statements in your code, but try to make them more useful by adding explanations like what you're expecting to see. An example from your own code: while count < primes: print 'entering the while loop...' if n < 3: print 'entering if n<3...' ## This denotes 2 as a prime count = count + 1 print 'the number of prime numbers found is "', count, '"', 'and the number just analyzed was "', n, '"' n = n + 1 As I said before, I prefer showing you how to debug rather than debug the code itself. It's the old "show you how to fish vs. give you a fish" thing. Learned it from my father.

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

    • Attachments:

Ask your own question

Ask a Question
Find 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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.