Jump to content

My Python Prime Number Automator

So I made this code a few months ago to calculate prime numbers. It automatically fetches the last number from the file and continues to calculate prime numbers from that point, meaning that you can start and restart the programs as many times as you want without having to worry about losing progress. It appends every prime number to the file so it is not totally useless. Currently it has calculated 157,899 prime numbers, which is all the ones up to 2,130,307. I have also done some other projects on developing a program to calculate prime numbers around 1X10 to the power of 32 and beyond (it takes a long time to calculate them though), I have also made a calculator which calculates prime numbers within a range. If anyone wants these or the text file with 157,899 prime numbers (this number will grow by the day) I can make them available. I run a modified version which was converted to an .exe by a Python extension which allows it to run in CMD, I'm pretty more resources are allocated to it in this version so you probably want to convert it yourself. So anyway here is the code.

file=open("PrimeNumbers.txt","r")read=file.readlines()y=read[-1]y=y.rstrip()y=int(y)y=y+1a=y+2000000file.close()i=1if y<2:    y=2for x in range (y,a):    c=2    r=x-1    loop=0    for loop in range (2,r):        y=x%loop        if y==0:            c=c+1        if c>2:            x=0    if c<=2:        print(x)        x=str(x)        x=x+"\n"        file=open("PrimeNumbers.txt","a")        if i==1:            file.write("\n")        file.write(x)        file.close()        i=0

It could probably be more efficient but I stopped working on the code several months ago. Please note that there is a bug with the program where it writes a blank line at the start of every execution, this bug can probably fixed but I just use Notepad++>TestFXEdit>Delete Blank Lines. You must also create a PrimeNumbers.txt file in the same folder before executing this code, it should have the number you want to start on in it. You're welcome to modify and use this code as you see fit, just please reference me and this post.

Thanks for reading!

| Cooler Master HAF 912 Plus | MSI P67A-G45 | i5 2500K @ 4.2GHz |  Coolermaster Hyper 212 Plus | EVGA GTX 670 FTW Edition 2GB | 8GB (2X4GB) Mushkin Blackline @ 1600MHz | 256GB OCZ Vertex 4 SSD | 1TB Western Digital Caviar Green | Corsair 600CX V2 | Windows 7 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

You could probably make it a lot more efficient and quicker if you loaded the file once with read/write permissions and wrote to it each time rather than loading it, writing one line and closing it again. Unless your file is massive, which it probably isn't yet.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

You could probably make it a lot more efficient and quicker if you loaded the file once with read/write permissions and wrote to it each time rather than loading it, writing one line and closing it again. Unless your file is massive, which it probably isn't yet.

yeah, the only problem I can see with that is that the program would have to have a defined end point, where as I prefer to just open and close it at my disposal, also my goal is to infinity so it doesn't really have a proper end.

| Cooler Master HAF 912 Plus | MSI P67A-G45 | i5 2500K @ 4.2GHz |  Coolermaster Hyper 212 Plus | EVGA GTX 670 FTW Edition 2GB | 8GB (2X4GB) Mushkin Blackline @ 1600MHz | 256GB OCZ Vertex 4 SSD | 1TB Western Digital Caviar Green | Corsair 600CX V2 | Windows 7 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

Uhh, the newline bug seems to be

if i==1:    file.write("\n")

because afterwards you have

i = 0

and nothing modifies 'i' afterwards. So there ya go.

 

And yeah, opening and closing the file repeatedly seems like a waste of time.

 

I also have a little script for finding prime numbers.

import sysn = int(sys.argv[1])for i in range(0, n + 1):	if (i % 2 is not 0 or i is 2) and (i % 3 is not 0 or i is 3) and (i % 5 is not 0 or i is 5) and (i % 7 is not 0 or i is 7):		print i

It's essentially an adaptation of the Sieve of Eratosthenes. I have to check if it is 2/3/5/7 since without it it will only work for i > 7.

 

Without changing any of the logic in your script, I came up with this. I didn't run it, since I don't have python installed on my Windows partition.

file      = open("PrimeNumbers.txt","a")content   = file.readlines()lastPrime = int(content[-1].rstrip()) + 1x         = lastPrime < 2 ? 2 : lastPrimey         = x + 2000000for i in range (x, y):    z = 2    for loop in range (2, i - 1):        x = i % loop        if x == 0:            z = z + 1        if z > 2:            i = 0    if z <= 2:        file.write(str(x) + '\n')file.close()
Link to comment
Share on other sites

Link to post
Share on other sites

I also have a little script for finding prime numbers.

import sysn = int(sys.argv[1])for i in range(0, n + 1):	if (i % 2 is not 0 or i is 2) and (i % 3 is not 0 or i is 3) and (i % 5 is not 0 or i is 5) and (i % 7 is not 0 or i is 7):		print i

 

I believe this code does not work as expected. To find prime numbers this way you have to test it against all prime numbers below sqrt of it and not just 2, 3, 5, and 7.

For instance 11*11 = 121 (hence, not a prime) and is not divisible by 2, 3, 5, or 7.

 

This post and the next give python implementations of trial division and the sieve to find primes.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×