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):

if primenum % 2 == 0:
return False

divisor = 3
prime = True

for x in range(primeroot):
if primenum % divisor == 0:
return False
prime = False
break
divisor += 2

if prime == True:
return True

divisor = 2
prime_num_generated = long(raw_input("Starting point(prime): "))
numbers_generated = long(raw_input("Amount of prime numbers to generate: "))
endpoint = 0
loopit = True
filename = 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
break

tofile.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: 589
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