import time
import queue
import threading
from fractions import gcd
THREADS = 2
def create_set_with_thread(t):
init = time.time()
set_ = list()
q = queue.Queue()
def _f():
while True:
x = q.get()
if gcd(x, t) == 1:
set_.append(x)
try:
q.task_done()
except ValueError:
break
for x in range(1, t):
q.put(x)
for num in range(THREADS):
thread = threading.Thread(target=_f)
thread.daemon = True
thread.start()
q.join()
print('threaded set created in', get_passed_time(init))
return set_
def create_set(t):
init = time.time()
out = []
for x in range(1, t):
if gcd(x, t) == 1:
out.append(x)
print('set created in', get_passed_time(init))
return out
def get_passed_time(cur_time):
cur_time = time.time() - cur_time
if cur_time > 60:
cur_time /= 60
label = 'mins'
else:
label = 'seconds'
return '%.5s %s' % (cur_time, label)
t = 10 ** 5
t0 = create_set_with_thread(t)
t1 = create_set(t)
print(len(t0), len(t1))
aW1wb3J0IHRpbWUKaW1wb3J0IHF1ZXVlCmltcG9ydCB0aHJlYWRpbmcKZnJvbSBmcmFjdGlvbnMgaW1wb3J0IGdjZAoKClRIUkVBRFMgPSAyCgoKZGVmIGNyZWF0ZV9zZXRfd2l0aF90aHJlYWQodCk6CiAgICBpbml0ID0gdGltZS50aW1lKCkKICAgIHNldF8gPSBsaXN0KCkKICAgIHEgPSBxdWV1ZS5RdWV1ZSgpCgogICAgZGVmIF9mKCk6CiAgICAgICAgd2hpbGUgVHJ1ZToKICAgICAgICAgICAgeCA9IHEuZ2V0KCkKICAgICAgICAgICAgaWYgZ2NkKHgsIHQpID09IDE6CiAgICAgICAgICAgICAgICBzZXRfLmFwcGVuZCh4KQogICAgICAgICAgICB0cnk6CiAgICAgICAgICAgICAgICBxLnRhc2tfZG9uZSgpCiAgICAgICAgICAgIGV4Y2VwdCBWYWx1ZUVycm9yOgogICAgICAgICAgICAgICAgYnJlYWsKCiAgICBmb3IgeCBpbiByYW5nZSgxLCB0KToKICAgICAgICBxLnB1dCh4KQoKICAgIGZvciBudW0gaW4gcmFuZ2UoVEhSRUFEUyk6CiAgICAgICAgdGhyZWFkID0gdGhyZ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