mqxym

primedata.py

Apr 14th, 2022
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. import sys
  2. import threading
  3. import hashlib
  4.  
  5. lower = 0
  6. upper = 20000
  7. limit = 1000 #How many primes you want to calculate
  8.  
  9. data = "" #Change Data when you want primes based on data
  10. computingPower = 8 #Every increase is increased by *16
  11.  
  12. enterData = 0
  13. repeatInput = 1 #If you want to keep inputing data
  14.  
  15. threadCount = 4
  16.  
  17. writeToFile = 0
  18. outputToScreen = 1
  19.  
  20. results = []
  21.  
  22.  
  23. def testInput ():
  24.     if lower > upper:
  25.         sys.exit("lower number is higher than upper number")
  26.  
  27.     if threadCount > 64 or threadCount < 1:
  28.         sys.exit("Thread counter wrong")
  29.  
  30.     if (lower <= 0):
  31.         sys.exit("Number can't be lover than 0")
  32.  
  33. def shuffleArray(lower, upper):
  34.     array = []
  35.     targetCount = round((upper-lower)/2)
  36.    
  37.     for i in range(targetCount):
  38.         array.append(lower+i)
  39.         array.append(upper-i)
  40.    
  41.     return array
  42.  
  43. def checkPrimes(array, start, end, threadID):
  44.     print("Thread start ID: ", threadID, " starts calculating")
  45.     global results,limit
  46.     for i in range(start,end):  
  47.         # all prime numbers are greater than 1
  48.         if(len(results) < limit):
  49.             checkPrime = array[i]
  50.             #Checks if Prime
  51.             for i in range(2, checkPrime):
  52.                 if (checkPrime % i) == 0:
  53.                     break
  54.             else:
  55.                 results.append(checkPrime)
  56.         else:
  57.             print("Thread ",threadID ," finished.", )
  58.             return
  59.  
  60.  
  61. def main():
  62.     global repeatInput
  63.  
  64.     while(repeatInput):
  65.  
  66.         threads = []
  67.         global data,lower,upper,threadCount,limit,results
  68.  
  69.         if (enterData):
  70.             print("Enter Data as Input or type exit")
  71.             data = input()
  72.             if (data == "exit"):
  73.                 sys.exit()
  74.             results = []
  75.         else:
  76.             repeatInput = 0
  77.  
  78.         if (len(sys.argv)==2):
  79.             data = sys.argv[1]
  80.  
  81.         if (data):
  82.             hash = hashlib.md5(data.encode())
  83.             hex = hash.hexdigest()
  84.             hex = hex[1:computingPower]
  85.             lower = int(hex,16)
  86.             upper = lower+10000
  87.             threadCount = 1
  88.             limit = 1
  89.  
  90.         print("Prime numbers between", lower, "and", upper, "are:")
  91.  
  92.         array = shuffleArray(lower, upper)
  93.         difference = round((upper - lower)/threadCount)
  94.  
  95.         for i in range(threadCount):
  96.             threadLow= difference*i
  97.             threadUp = difference * (i+1)
  98.  
  99.             thread = threading.Thread(target=checkPrimes, args=(array,threadLow, threadUp,i))
  100.            
  101.             threads.append(thread)
  102.  
  103.         for thread in threads:
  104.             thread.start()
  105.  
  106.         for thread in threads:
  107.             thread.join()
  108.  
  109.         print("Calculating finished")
  110.         output = sorted(results)
  111.  
  112.         if (outputToScreen):
  113.             print (output)
  114.  
  115.         if (writeToFile):
  116.             with open('out.txt', 'w') as f:
  117.                 for prime in output:
  118.                     write = str(prime) + "\n"
  119.                     f.write(write)
  120.  
  121. if __name__ == "__main__":
  122.     main()
Add Comment
Please, Sign In to add comment