Here's the question you clicked on:
SgtRock
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)
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'
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 ;)
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.
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.
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.