Python Prime Number Generator

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

Python Prime Number Generator

Post by Arrexel on Wed Feb 25, 2009 3:26 pm
([msg=18665]see Python Prime Number Generator[/msg])

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 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
User avatar
Arrexel
New User
New User
 
Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Location: Ontario, Canada
Blog: View Blog (0)


Re: Python Prime Number Generator

Post by TheMindRapist on Wed Feb 25, 2009 3:41 pm
([msg=18666]see Re: Python Prime Number Generator[/msg])

It's the same ways to make it efficient as your first program had, only you test more numbers.
Image
User avatar
TheMindRapist
Contributor
Contributor
 
Posts: 585
Joined: Mon Apr 14, 2008 4:57 pm
Blog: View Blog (0)


Re: Python Prime Number Generator

Post by Arrexel on Thu Feb 26, 2009 3:48 pm
([msg=18736]see Re: Python Prime Number Generator[/msg])

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
User avatar
Arrexel
New User
New User
 
Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Location: Ontario, Canada
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests