Skip to content

Instantly share code, notes, and snippets.

@Ahmed
Last active December 15, 2015 21:59
Show Gist options
  • Save Ahmed/5329260 to your computer and use it in GitHub Desktop.
Save Ahmed/5329260 to your computer and use it in GitHub Desktop.
Prime Number Checker
# by Ahmed Hassan
import random
from math import log
def powerMod(a,n,m):
num_roots_operation = int(log(n)/log(2))
all_roots_calc =[]
all_roots_calc.append(a)
count = 0
root = (a*a) % m
while count != num_roots_operation:
all_roots_calc.append(root)
root = (root * root) % m
count = count+1
bin_opr= []
N=n
while N != 0:
bin_opr.append(N%2)
N = N /2
modulo = 1
while bin_opr != [] :
if bin_opr[0] == 1:
modulo = (modulo * all_roots_calc[0] ) % m
bin_opr.pop(0)
all_roots_calc.pop(0)
return modulo
def randomInt():
while True:
ZZ = int((random.random()*1000)**(random.random()*10))
if ZZ > 2:
break
return ZZ
def gcd(a,b):
while b!=0:
t = b
b = a % b
a = t
return a
def primeNumberCheck(x):
N = x
if x < 100:
for i in range(1,x):
if gcd(x,i) != 1:
return str(x) + " is not prime"
return str(x) + " is prime"
while N != 0:
RndINT = randomInt()
if powerMod(RndINT,x-1,x) == 0:
continue
if powerMod(RndINT,x-1,x) != 1 :
return str(x) + " is not prime"
N = int(N/10.)
return str(x) + " is prime"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment