GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — algorithms/maths/rabin_miller.py at master · marcosfede/algorithms Expand file tree/ Copy pathrabin_miller.pyMore file actions 50 lines (36 loc) · 994 Bytes / Copy pathrabin_miller.pyFile metadata and controls50 lines (36 loc) · 994 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 import random # factor n into a power of 2 times an odd number def pow2_factor(n): power = 0 while n % 2 == 0: n /= 2 power += 1 return power, n """ Rabin-Miller primality test returning False implies that n is guarenteed composite returning True means that n is probably prime with a 4 ** -k chance of being wrong """ def is_prime(n, k): r, d = pow2_factor(n - 1) """ returns true if a is a valid 'witness' for n a valid witness increases chances of n being prime an invalid witness guarentees n is composite """ def valid_witness(a): x = pow(a, d, n) if x == 1 or x == n - 1: return False for _ in range(r - 1): x = pow(x, 2, n) if x == 1: return True if x == n - 1: return False return True for _ in range(k): if valid_witness(random.randrange(2, n - 2)): return False return True |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |