Last active
January 2, 2016 02:18
-
-
Save myrkvi/8235678 to your computer and use it in GitHub Desktop.
Prime numbers and shit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import clock | |
import sys | |
def percentageCompleted(cur, tot): | |
p = (cur + .0) / (tot + .0) * 100 | |
return "%.2f" % p | |
def isPrime(num): | |
if num <= 1: | |
return "" | |
elif num == 2: | |
return "2\n" | |
else: | |
for x in xrange(2, num/2): | |
if num % x == 0: | |
return "" | |
return str(num) + "\n" | |
f = file("./primes.txt", "w") | |
f.write("2\n") | |
def checkPrimes(upTo): | |
x = 0 | |
for x in xrange(3, upTo + 1, 2): | |
f.write( isPrime(x) ) | |
x = x + 1 | |
if x % 1000 == 0: | |
s = percentageCompleted(x, upTo) + "%\t\t" + ( "%.2fs\r" % clock() ) | |
sys.stdout.write(s) | |
f.close() | |
if __name__ == "__main__": | |
checkPrimes(999999) | |
x = raw_input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment