Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import itertools
- import functools
- import sys
- from collections import OrderedDict
- import time
- from numba import jit
- isprime = lambda number: all(number % i for i in range(2, number))
- # @jit(nopython=True)
- def is_prime(number):
- n = number
- counter = 0
- for i in range(1, n + 1):
- if n % i == 0:
- counter += 1
- return True if counter == 2 else False
- def get_primes(start, edge):
- primes = []
- t = time.clock()
- for i in range(start, edge):
- if i % 1000 == 0: print(i)
- if is_prime(i): primes.append(i)
- print ("primes: "+len(primes).__str__(), ' for ', (time.clock() - t))
- return primes
- statement = jit(nopython=True)(lambda x, n, rest: n**x % rest)
- post_statement = jit(nopython=True)(lambda a, b, rest: a**b % rest)
- class Private(object):
- def __init__(self, n, rest):
- self.rest = rest
- self.n = n
- ## statement = lambda self, x: statement(x, self.n, self.rest)
- ## post_statement = lambda self, a, b: statement(a, b, self.rest)
- statement = (lambda self, x: (self.n**x) % self.rest)
- post_statement = (lambda self, a, b: (a**b) % self.rest)
- RAND = 99
- # by new: 99/(378, 83) == 4% (79, 83) 4.5%
- def cry_test(secure):
- skey = random.randint(2 ,RAND)
- open_skey = secure.statement(skey)
- # client:
- rkey = random.randint(2, RAND)
- r_open_key = secure.statement(rkey)
- psw = secure.post_statement(open_skey, rkey)
- # server
- spsw = secure.post_statement(r_open_key, skey)
- # print psw
- return psw, skey, rkey
- def unit_test(i,j):
- secure = Private(i,j)
- n = 1000
- l = []; sk = []; rk = []
- for k in range(n):
- a,b,c = cry_test(secure)
- l.append(a)
- return [int(l.count(c))/n*100 for c in range(j)]
- def start_test():
- dct = {}
- t = time.clock()
- n = 100
- rest = 100
- primes = get_primes(62, rest)
- for j in primes: # range(1,n):
- for i in range(n):
- dct[(i,j)] = unit_test(i,j)
- # print ( max(dct[(i,j)]).__str__() + '% - ' + (i,j).__str__())
- print(j)
- print(time.clock() - t)
- if sys.version_info.major == 2:
- rez = {k:max(v) for k, v in dct.iteritems()}
- items = list(rez.iteritems())
- else:
- rez = {k:max(v) for k, v in dct.items()}
- items = list(rez.items())
- items.sort(key=lambda i: i[1])
- od = OrderedDict()
- cnt = 0
- for item in items:
- if item[1] > 5:
- cnt+=1
- continue
- key = str(item[1]) + '%'
- if key not in od:
- od[key] = [item[0]]
- else:
- od[key].append(item[0])
- # od[str(item[1]) + '%'] = item[0]
- print (od)
- print(len(primes)*n - cnt, ' из ', len(primes)*n)
- return od
- # print time.clock() - t
- # print [sk.count(c) for c in range(67)]
- # print [rk.count(c) for c in range(67)]
- md = []
- for i in range(3):
- print('step ' + i.__str__())
- md.append(start_test())
- time.sleep(1)
- avr = {}
- for od in md:
- for k,vals in od.items():
- # print ('...........')
- for val in vals:
- if val in avr:
- # print( '-------------')
- avr[val].append(k)
- else:
- # print('+++++++++++++')
- avr[val] = [k]
- aod = OrderedDict()
- items = list(avr.items())
- items.sort(key=lambda i: [float(j.replace('%','')) for j in i[1]])
- for k,v in items:
- if len(v)>1: aod[k]=v
- print (aod)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement