Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from fractions import gcd
- from time import time
- from decimal import Decimal
- import threading, _thread
- from primesieve import generate_primes
- hard_start = 2
- hard_end = 80000
- class CustomThread(threading.Thread):
- def __init__(self, func, start, end, filename):
- threading.Thread.__init__(self)
- self.func = func
- self.begin = start
- self.end = end
- self.filename = filename+" {} to {}".format(self.begin, self.end)
- self.primes = generate_primes(self.begin, self.end)
- def repeat_write(self, html=False):
- total_time = 0
- if html:
- with open(self.filename+'.html', 'w+') as file:
- file.write("""<!DOCTYPE html>
- <HTML>
- <Head>
- </Head>
- <Body>
- <table>
- <tr>
- <th>Number (N)</th>
- <th>Seconds</th>
- <th>result</th>
- </tr>
- """)
- for x in range(self.begin,self.end):
- if x in self.primes or x%2 == 0:
- pass
- else:
- secs, result = self.func(x)
- text = """
- <tr>
- <th>{}</th>
- <th>{}</th>
- <th>{}</th>
- </tr>""".format(x, secs, result)
- file.write(text)
- total_time+=secs
- file.write("""
- </table>
- Total time was: {} seconds
- </Body>
- </HTML>
- """.format(total_time))
- else:
- with open(self.filename+'.txt', 'w+') as file:
- file.write("""Number, Seconds, result""")
- for x in range(self.begin,self.end):
- if x in self.primes or x%2 == 0:
- pass
- else:
- secs, result = self.func(x)
- text = """
- {}, {}, {}""".format(x, secs, result)
- file.write(text)
- total_time+=secs
- def run(self):
- self.repeat_write()
- def timeit(func):
- def inner(*args,**kwargs):
- start = time()
- result = func(*args, **kwargs)
- end=time()
- ellapsed = end - start
- return (ellapsed, result)
- return inner
- @timeit
- def factorise(N):
- N=Decimal(N)
- for i in range(2,int(N.sqrt())+2):
- if N/i == N//i:
- factor=i
- break
- else:
- return 0
- return [factor, int(N)//factor]
- @timeit
- def p_rho_algorithm(N, func = lambda x: x**2 + 1):
- x=y=2
- d=1
- while d==1:
- x = func(x)%N
- y = func(func(y)%N)%N
- d = gcd(abs(x-y), N)
- else:
- return [d, N//d]
- return 0
- thread1 = CustomThread( factorise, hard_start, hard_end, 'factorise')
- thread2 = CustomThread( p_rho_algorithm, hard_start, hard_end, 'p_rho_algorithm')
- thread1.start()
- thread2.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement