0

I recently started messing around with python and I wrote a program to print out the 1000th prime number but the output only shows a blinking cursor , the code is shown below:

number = 3
count= 1

while count <= 1000:   
          prime = True
          for x in range(2, number):
          if number % x == 0:
               prime= False
          if prime == True:
               count = count + 1
          if count <= 1000:
               number = number + 1

print number

Any help and concise explanation would be appreciated

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Desmond
  • 3
  • 2

1 Answers1

1

edit: i just realized the problem. @tichodroma solved the problem but did so by editing the OP post. so when i got to it it was already solved, how ever, he solved it by putting the print into the loop, hence the many numbers waterfall. but it should be outside the loop so as to only show the final result. also - after looking at the OP code before edit, it was written in such a way that it was taking a long time to run, and the "blinking line" was the system working in the background

def isprime(n):
    '''check if integer n is a prime'''
    # make sure n is a positive integer
    n = abs(int(n))
    # 0 and 1 are not primes
    if n < 2:
        return False
    # 2 is the only even prime number
    if n == 2: 
        return True    
    # all other even numbers are not primes
    if not n & 1: 
        return False
    # range starts with 3 and only needs to go up the squareroot of n
    # for all odd numbers
    for x in range(3, int(n**0.5)+1, 2):
        if n % x == 0:
            return False
    return True

counter = 0
number = 0

while True:
    if isprime(number):
        counter+=1
    if counter == 10000:
        break
    number+=1

print number
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • the "isprime" code is from "http://www.daniweb.com/software-development/python/code/216880/check-if-a-number-is-a-prime-number-python" which was from a quick google search. but there are simpler methods to determine. – Inbar Rose Jul 31 '12 at 07:52
  • the question is not about how to find 1000th prime (in this case a simple [Sieve of Eratosthenes would be better](http://stackoverflow.com/questions/11641098/interpreting-a-benchmark-in-c-clojure-python-ruby-scala-and-others#comment15487214_11641098)). The question is why the OP sees only blinking cursor. – jfs Jul 31 '12 at 07:53
  • when i ran the OP code, it started printing numbers from 3 increasing by 1 each time on a new line, until i stopped the code. – Inbar Rose Jul 31 '12 at 07:55
  • yes. That's why I've added [comment to the question](http://stackoverflow.com/questions/11735357/python-output-just-shows-blinking-cursor/11735457#comment15573900_11735357). – jfs Jul 31 '12 at 07:57
  • true, i see that now, when i started looking to answer the question there were no comments. – Inbar Rose Jul 31 '12 at 07:59
  • i just realized the problem. @tichodroma solved the problem but did so by editing the OP post. so when i got to it it was already solved, how ever, he solved it by putting the print into the loop, hence the many numbers waterfall. but it should be outside the loop so as to only show the final result. also - after looking at the OP code before edit, it was written in such a way that it was taking a long time to run, and the "blinking line" was the system working in the background. – Inbar Rose Jul 31 '12 at 08:07
  • 1
    btw, `isprime` should probably return an error if it doesn't receive a positive integer as an argument otherwise it might hide an error in other parts of a program. On Python 2.x if `n` may be large than `xrange` should be used. `int(n**.5) + 1` should be replaced by `int(n**.5 + 1.5)` (check `n==3` case). – jfs Jul 31 '12 at 08:15
  • @ J.F. Sebastian. I showed the exact code I used, the logic of the code seems right but why isn't it running – Desmond Jul 31 '12 at 17:18
  • @InbarRose Rose the code you posted above does not actually work, and I actually want to know why my own code does not work. – Desmond Jul 31 '12 at 17:20
  • @ Joel Corenett , Yes I'm using the IDLE – Desmond Jul 31 '12 at 18:05
  • @Inbar Rose , you're right it takes a while to load, I just need to wait for 5 secs. – Desmond Aug 01 '12 at 02:01