Last active
December 24, 2015 20:19
-
-
Save Taiiwo/6856907 to your computer and use it in GitHub Desktop.
A python script that will find all prime numbers between 2 numbers. Bugs: Won't find primes under 11. Does some extra checking and needs to be sped up.
This file contains hidden or 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
import sys, thread, time | |
# define some settings | |
threads = 4 | |
logname = "log.log" | |
def getprimes(rangemin, rangemax, log): # This function returns all primes between | |
# rangemin and rangemax, and puts them into log | |
if rangemin % 2 == 0: # if number is divisible by 2 | |
rangemin += 1 #make odd | |
while rangemin < rangemax: #make sure we don't search over the specified number | |
i = 3 #start at 3 | |
rangemin += 2 #add 2 because we want the number to stay odd | |
while i <= rangemin/3:# while we're under a third of ourself | |
#because no odd number is divisible by 2 | |
#and primes are allowed to be divisible | |
#by one | |
if rangemin % i == 0:# if the number is divisible by the | |
#number we're iterating | |
break #stop, and try again | |
else: | |
i += 2# else, continue, and try the next odd number | |
if i > rangemin/3: # if we've iterated all the way up to 'i' | |
# but we didn't break | |
log.write(str(rangemin) + '\n')# log dat prime | |
print str(rangemin) + '\n'# print dat prime | |
#continue to loop, and find more primes | |
print 'Thread completed' | |
# getprimes(int(sys.argv[1]),int(sys.argv[2])) | |
num1 = int(sys.argv[1]) | |
num2 = int(sys.argv[2]) | |
i = threads | |
log = open (logname, 'a') | |
while i >= 1:# iterate for each thread | |
arg1 = num1 + ((num2 - num1)/threads)*(i-1)# calculate how many numbers to give | |
arg2 = num1 + ((num2 - num1)/threads)*i# each core (Max and min) | |
thread.start_new_thread( getprimes, (arg1, arg2, log))# run getprimes in a new | |
#thread for each thread | |
#with our calulated | |
#max and min values | |
i -= 1# take one away from the thread to continue iterating downwards | |
while 1:# wait forever for primes to print | |
pass |
This file contains hidden or 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
import sys, thread, time | |
threads = 4 | |
def getprimes(rangemin, rangemax): | |
if rangemin % 2 == 0:# make num odd | |
rangemin += 1 | |
while rangemin < rangemax: | |
i = 3 | |
rangemin += 2 | |
while i <= rangemin/3: | |
if rangemin % i == 0: | |
break | |
else: | |
i += 1 | |
if i > rangemin/3: | |
print rangemin | |
# getprimes(int(sys.argv[1]),int(sys.argv[2])) | |
num1 = int(sys.argv[1]) | |
num2 = int(sys.argv[2]) | |
i = threads | |
while i >= 1: | |
#thread.start_new_thread( getprimes, (num1 + (num2 - num1)/i,(num2 - num1)/(i-1))) | |
thread.start_new_thread( getprimes,(num1 + ((num2 - num1)/threads)*(i-1), num1 + ((num2 - num1)/threads)*i )) | |
i -= 1 | |
input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import sys
prime_count = 1
start_number = 2
number_to_check = 2
while prime_count <= 100:
result = number_to_check % start_number