Last active
February 23, 2020 08:39
-
-
Save primus-lab/daae0a2ae92bf81fd7753c1e529f2b2d to your computer and use it in GitHub Desktop.
Conjectured primality test for numbers of the form 2kp^n +/- 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
This script was written in python 3.x. | |
In order to run this script, please make sure your python version is 3.x or above. | |
How to run: | |
python LLRT.py | |
or if it doesn't work use this one: | |
python3 LLRT.py | |
Author: Pedja Terzic <[email protected]> | |
''' | |
from sympy import * | |
from mpmath import * | |
from tkinter import * | |
import tkinter.messagebox | |
from tkinter.ttk import Frame, Label, Entry, Radiobutton, Button, Style | |
mp.dps = 50000; mp.pretty = True | |
class LLRT(Frame): | |
def __init__(self, parent): | |
Frame.__init__(self, parent) | |
self.parent = parent | |
self.initUI() | |
def initUI(self): | |
self.parent.title("LLRT") | |
self.pack(fill=BOTH, expand=True) | |
global value | |
value = 0 | |
global v | |
v = IntVar() | |
v.set(1) | |
global coeff | |
coeff = StringVar() | |
global base | |
base = StringVar() | |
global exp | |
exp = StringVar() | |
global res | |
res = StringVar() | |
frame1 = Frame(self,style='My.TFrame') | |
frame1.pack(fill=X) | |
rb1 = Radiobutton(frame1, text = "2*k*p^n-1", variable = v, value = 1,style='My.TRadiobutton') | |
rb1.pack( anchor = W ) | |
rb2 = Radiobutton(frame1, text = "2*k*p^n+1", variable = v, value = 2,style='My.TRadiobutton') | |
rb2.pack( anchor = W ) | |
frame2 = Frame(self,style='My.TFrame') | |
frame2.pack(fill=X) | |
lbl2 = Label(frame2, text="k:", width=2,background='orange') | |
lbl2.pack(side=LEFT, padx=5, pady=5) | |
entry2 = Entry(frame2,textvariable=coeff,style='My.TEntry') | |
entry2.pack(fill=X, padx=5, expand=True) | |
frame3 = Frame(self,style='My.TFrame') | |
frame3.pack(fill=X) | |
lbl3 = Label(frame3, text="p:", width=2,background='orange') | |
lbl3.pack(side=LEFT, padx=5, pady=5) | |
entry3 = Entry(frame3,textvariable=base,style='My.TEntry') | |
entry3.pack(fill=X, padx=5, expand=True) | |
frame4 = Frame(self,style='My.TFrame') | |
frame4.pack(fill=X) | |
lbl4 = Label(frame4, text="n:", width=2,background='orange') | |
lbl4.pack(side=LEFT, padx=5, pady=5) | |
entry4 = Entry(frame4,textvariable=exp,style='My.TEntry') | |
entry4.pack(fill=X, padx=5, expand=True) | |
frame5 = Frame(self,style='My.TFrame') | |
frame5.pack(fill=X) | |
result = Label(frame5, textvariable=res, width=70,background='orange') | |
result.pack(side=LEFT, padx=30, pady=5) | |
frame6 = Frame(self,style='My.TFrame') | |
frame6.pack(fill=X) | |
btntest = Button(frame6, text="Check", width=8, command=self.test,style='My.TButton') | |
btntest.pack(side=LEFT, anchor=N, padx=5, pady=5) | |
btnclear = Button(frame6, text="Clear", width=8, command=self.clear,style='My.TButton') | |
btnclear.pack(side=LEFT, anchor=N, padx=5, pady=5) | |
btnclose = Button(frame6, text="Close", width=8, command=self.quit,style='My.TButton') | |
btnclose.pack(side=LEFT, anchor=N, padx=5, pady=5) | |
def errorMsg(self,msg): | |
if msg == 'error': | |
tkinter.messagebox.showerror('Error!', 'Something went wrong! Maybe invalid entries!') | |
elif msg == 'errc': | |
tkinter.messagebox.showerror('Error!', '2k must be less than 2^n!') | |
elif msg == 'errb': | |
tkinter.messagebox.showerror('Error!', 'p must be greater than one!') | |
elif msg == 'erre': | |
tkinter.messagebox.showerror('Error!', 'n must be greater than two!') | |
elif msg == 'errd': | |
tkinter.messagebox.showerror('Error!', 'p must be a prime number!') | |
def test(self): | |
try: | |
k = int(coeff.get()) | |
p = int(base.get()) | |
n = int(exp.get()) | |
if 2**n<=k: | |
self.errorMsg('errc') | |
elif p<2: | |
self.errorMsg('errb') | |
elif n<3: | |
self.errorMsg('erre') | |
elif not(isprime(p)): | |
self.errorMsg('errd') | |
else: | |
def polynomial(m,x): | |
if m==1: | |
return x | |
else: | |
p0=2 | |
p1=x | |
l=2 | |
while l<=m: | |
p=x*p1-p0 | |
p0=p1 | |
p1=p | |
l=l+1 | |
return p | |
def jacobi(a,q): | |
j=1 | |
while a != 0: | |
while a%2==0: | |
a=a/2 | |
if q%8==3 or q%8==5: | |
j=-j | |
#interchange(a,q) | |
c=a | |
a=q | |
q=c | |
if a%4==3 and q%4==3: | |
j=-j | |
a=fmod(a,q) | |
if q==1: | |
return j | |
else: | |
return 0 | |
if v.get()==1: | |
M=2*k*p**n-1 | |
d=3 | |
while not(jacobi(d-2,M)==1 and jacobi(d+2,M)==-1): | |
d=d+1 | |
s=polynomial(k*p**2,d)%M | |
ctr=1 | |
while ctr<=n-2: | |
s=polynomial(p,s)%M | |
ctr=ctr+1 | |
if int(s)==M-2: | |
value=str(2*k) + "*" + str(p) + "^" + str(n) + "-1 is prime" | |
res.set(self.makeAsItIs(value)) | |
else: | |
value=str(2*k) + "*" + str(p) + "^" + str(n) + "-1 is composite" | |
res.set(self.makeAsItIs(value)) | |
else: | |
N=2*k*p**n+1 | |
d=3 | |
while not(jacobi(d-2,N)==-1 and jacobi(d+2,N)==-1): | |
d=d+1 | |
s=polynomial(k*p**2,d)%N | |
ctr=1 | |
while ctr<=n-2: | |
s=polynomial(p,s)%N | |
ctr=ctr+1 | |
if int(s)==N-2: | |
value=str(2*k) + "*" + str(p) + "^" + str(n) + "+1 is prime" | |
res.set(self.makeAsItIs(value)) | |
else: | |
value=str(2*k) + "*" + str(p) + "^" + str(n) + "+1 is composite" | |
res.set(self.makeAsItIs(value)) | |
except: | |
self.errorMsg('error') | |
def clear(self): | |
try: | |
res.set('') | |
coeff.set('') | |
base.set('') | |
exp.set('') | |
except: | |
self.errorMsg('error') | |
def makeAsItIs(self, value): | |
return value | |
def main(): | |
root = Tk() | |
root.resizable(0,0) | |
s = Style() | |
s.configure('My.TFrame', background='orange') | |
s.configure('My.TButton', background='light gray') | |
s.configure('My.TEntry', fieldbackground='light gray') | |
s.configure('My.TRadiobutton', background='orange') | |
s.map('My.TRadiobutton', background=[('active', '#FFC133')]) | |
root.geometry("250x177") | |
llrt = LLRT(root) | |
root.mainloop() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment