## Python Prime Number Generator

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

### Python Prime Number Generator

Also made this code while bored. My suggestion for a code database, which is being thought about, is now becoming more apparent.

Code: Select all
`def isprime(n):    primenum = float(n)    if primenum % 2 == 0:        return False    primeroot = int(primenum**.5)    divisor = 3    prime = True    for x in range(primeroot):        if primenum % divisor == 0:            return False            prime = False            break        divisor += 2    if prime == True:        return Truedivisor = 2prime_num_generated = long(raw_input("Starting point(prime): "))numbers_generated = long(raw_input("Amount of prime numbers to generate: "))endpoint = 0loopit = Truefilename = str(prime_num_generated) + "+" + str(numbers_generated) + ".txt"tofile = open(filename, "w")while loopit == True:    if isprime(prime_num_generated) == True:        print prime_num_generated        endpoint += 1        filenum = str(prime_num_generated) + "\n"        tofile.write(filenum)    prime_num_generated += 2    if endpoint == numbers_generated:        loopit = False        breaktofile.close`

Again, it could probably be more efficient.
"I think there is a world market for maybe five computers."
Unverified Quote, 1945 - Thomas J. Watson, Founder of IBM

Arrexel
New User

Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Blog: View Blog (0)

### Re: Python Prime Number Generator

It's the same ways to make it efficient as your first program had, only you test more numbers.

TheMindRapist
Contributor

Posts: 585
Joined: Mon Apr 14, 2008 4:57 pm
Blog: View Blog (0)

### Re: Python Prime Number Generator

Ah, correct again. Although I did write this program before I had a chance to read your suggestion in my other post
"I think there is a world market for maybe five computers."
Unverified Quote, 1945 - Thomas J. Watson, Founder of IBM

Arrexel
New User

Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Blog: View Blog (0)

### Who is online

Users browsing this forum: No registered users and 0 guests