def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.setWindowTitle('Encryption') self.setupUi(self) self.events(self) # RSA AUTO SETTINGS d = -1 while d < 0: p = func.random_n(10, 100) q = func.random_n(10, 100) while len(str(p)) != len(str(q)): q = func.random_n(10, 100) while p == q: q = func.random_n(10, 100) self.lineEdit.setText(unicode(p)) self.lineEdit_2.setText(unicode(q)) n = p * q key = (p - 1) * (q - 1) e = func.random_n(0, 10) self.lineEdit_3.setText(unicode(e)) while func.gcd(key, e) != 1: e = func.random_n(0, 10) tmp = func.egcd(e, key) d = int(tmp[1]) # EL-GAMAL AUTOSETTINGS p = func.random_n(30, 100) # value 1 g = random.randint(1, 1000) # value 2 x = random.randint(1, 1000) # value 3 while g > p - 1: g = random.randint(1, 1000) while x > p: x = random.randint(1, 1000) k = random.randint(1, 1000) # value 4 while func.gcd(k, p - 1) != 1: k = random.randint(1, 1000) self.lineEdit_4.setText(unicode(p)) self.lineEdit_5.setText(unicode(g)) self.lineEdit_6.setText(unicode(x)) self.lineEdit_7.setText(unicode(k)) # Rabin AUTO SETTINGS p, q = func.rand34() # value 1,2 self.lineEdit_8.setText(unicode(p)) self.lineEdit_9.setText(unicode(q))
def f(y, x=1): keys = set() found = False while y > 1: if (x, y) in memo: found = True val = memo[(x, y)] break keys.add((x, y)) _gcd = gcd(x, y) if _gcd != 1: x //= _gcd y //= _gcd else: x += 1 y -= 1 keys.add((x, y)) for k in keys: if k not in memo: if found: memo[k] = val else: memo[k] = x if found: return val return x
def properFraction(pNume,pDeno): '''pNume: numerator, pDeno: Denominator return [z,x,y] such that pNume/pDeno =z + x/y and gcd(x,y) = 1''' t = gcd(pDeno,pNume) z = pNume / pDeno x,y = (pNume - z*pDeno)/t, pDeno/t return [z,x,y]
def gen_triples(N): for m in range(1, int(N**.5) + 1): for n in range(1, m): if (m - n) % 2 and gcd(m, n) == 1: c = m**2 + n**2 if c <= N: a = m**2 - n**2 b = 2 * m * n yield (a, b, c)
def gen_triples(N): for m in range(1, int(N**.5) + 1): # 1 for n in range(1, m): # 2 if (m - n) % 2 and gcd(m, n) == 1: # 3 c = m**2 + n**2 # 4 if c <= N: # 5 a = m**2 - n**2 # 6 b = 2 * m * n # 7 yield (a, b, c) # 8
def gen_triples(N): for m in range(1, int(N**.5) + 1): #1 for n in range(1, m): #2 if (m - n) % 2 and gcd(m, n) == 1: #3 c = m**2 + n**2 #4 if c <= N: #5 a = m**2 - n**2 #6 b = 2 * m * n #7 yield (a, b, c) #8
def main(): s=0 for a in range(2,D/2+1): for b in range(2*a+1,min(3*a,D+1)): if functions.gcd(a,b) == 1: # print 'add:',a,b s+=1 # else: print 'delete',a,b print 'answer:',s
def continued_fraction(x): """ Returns the continued fraction of x as a list of tuples (a,b,c), where: a + (sqrt(x)+b / c) represents each step. If we are interested on the notation of the form [4;(1,3,1,8)], we must take the first value from each tuple. Note: Be careful if the value passed is a perfect square. """ res = [] a = floor(x**0.5) b = -floor(x**0.5) c = 1 t = (a,b,c) res.append(t) while True: a, b, c = res[-1][0], res[-1][1], res[-1][2] a1 = floor(c*(x**0.5 - b)/(x - b*b)) b1 = -b * c c1 = x - b*b b1 -= a1*c1 _gcd = gcd(b1, c1) _gcd = gcd(_gcd, c) b1 //= _gcd c1 //= _gcd t = (a1, b1, c1) if t in res: # All continued fractions are periodic res.append(t) break res.append(t) return res
def continued_fraction(x): """ Returns the continued fraction of x as a list of tuples (a,b,c), where: a + (sqrt(x)+b / c) represents each step. If we are interested on the notation of the form [4;(1,3,1,8)], we must take the first value from each tuple. Note: Be careful if the value passed is a perfect square. """ res = [] a = floor(x**0.5) b = -floor(x**0.5) c = 1 t = (a, b, c) res.append(t) while True: a, b, c = res[-1][0], res[-1][1], res[-1][2] a1 = floor(c * (x**0.5 - b) / (x - b * b)) b1 = -b * c c1 = x - b * b b1 -= a1 * c1 _gcd = gcd(b1, c1) _gcd = gcd(_gcd, c) b1 //= _gcd c1 //= _gcd t = (a1, b1, c1) if t in res: # All continued fractions are periodic res.append(t) break res.append(t) return res
import math from sympy import * from functions import gcd def multiplyList(myList): result = 1 for x in myList: result = result * x return result res = [] size = 120000 for c in range(3, size): for b in range(math.ceil(c / 2), c): a = c - b if a < b < c and gcd(b, c) == 1 and gcd(a, c) == 1 and gcd( a, b) == 1 and multiplyList(factorint(a * b * c)) < c: print(a, b, c) res.append((a, b, c)) print(len(res)) print(sum(t[2] for t in res)) # todo overtime
from functions import pythTriplet, gcd LIMIT = 100000000 solutions = 0 for m in range(2, int(LIMIT**0.5) + 1): for n in range(1, m): if (m + n) % 2 == 0: continue if gcd(m, n) != 1: continue t = pythTriplet(m, n) # Reorder triplet if t[0] > t[1]: t[0], t[1] = t[1], t[0] if (t[2] % (t[1] - t[0])) == 0: perimeter = sum(t) solutions += LIMIT // perimeter print(solutions)
""" The triangle 5,5,6 is a triangle in which there is a pythagorean triplet such that 3,4,5 in which there is a number n in [3,4] (not the diagonal) such that 5-2n == 1 or 5-2n == -1 Therefore, we want to generate Pythagorean triplets in which mm + nn - min(mm - nn, 2mn) == 1 or -1 We are only interested in primitive ones because the difference is increased by a factor of d. Also, the perimeter is 2mm + 2mn """ from functions import pythTriplet, gcd total_perimeter = 0 for m in range(2, 20000): for n in range(1, m): if (m + n)%2 == 0: continue if gcd(m, n) != 1: continue t = pythTriplet(m, n) if abs(t[2] - 2*t[1]) == 1 or abs(t[2] - 2*t[0]) == 1: total_perimeter += 2*(t[2] + min(t[0], t[1])) print(t, 'PER', total_perimeter)
""" ------------------------------------------------------- [program description] ------------------------------------------------------- Author: Max Dann ID: 190274440 Email: [email protected] __updated__ = "2020-02-11" ------------------------------------------------------- """ from functions import gcd print(gcd(9,6))
import functions for i in range(0, 10000): functions.fib(20) for i in range(0, 10000): functions.gcd(1234, 84, 123) for i in range(0, 10000): functions.recursiveAdd(10, 3)
from functions import gcd N = 50 triples = sorted( #1 ( (a, b, c) for a, b, c in ( #2 ((m**2 - n**2), (2 * m * n), (m**2 + n**2)) #3 for m in range(1, int(N**.5) + 1) #4 for n in range(1, m) #5 if (m - n) % 2 and gcd(m, n) == 1 #6 ) if c <= N), key=lambda *triple: sum(*triple) #7 ) print(triples)
from functions import gcd N = 50 triples = sorted( # 1 ((a, b, c) for a, b, c in ( # 2 ((m**2 - n**2), (2 * m * n), (m**2 + n**2)) # 3 for m in range(1, int(N**.5) + 1) # 4 for n in range(1, m) # 5 if (m - n) % 2 and gcd(m, n) == 1 # 6 ) if c <= N), key=lambda *triple: sum(*triple) # 7 ) print(triples)
def dh(): d = -1 print "Do you want manualy enter settings? (y/n)" manual = str(raw_input("$ ")) if manual == "n": while d<0: p = func.random_n(10,100) q = func.random_n(10,100) while len(str(p)) != len(str(q)): q = func.random_n(10,100) while p == q: q = func.random_n(10,100) n = p*q key = (p-1)*(q-1) e = func.random_n(0,10) while func.gcd(key,e) != 1: print "[ DH ] e generation failure, trying another e..." e = func.random_n(0,10) tmp = func.egcd(e,key) d = int(tmp[1]) print "[ DH ] p =",p,"q =",q print "[ DH ] n =",n print "[ DH ] e =",e,"key =",key print "[ DH ] d =",d elif manual == "y": p = int(input("p = ")) q = int(input("q = ")) n = p*q key = (p-1)*(q-1) e = int(input("e = ")) tmp = func.egcd(e,key) d = int(tmp[1]) print "[ DH ] Choose 'encode' or 'decode' for proceed, or exit for quit" method = str(raw_input("$ ")) while len(method) > 1: if method == "exit": os._exit(1) if method == "encode": text = str(raw_input("input text [encode] $ ")) start_time = time.time() size = len(text) print "[ DH ] text lenght:", size text_int = [] i = 0 while i < size: text_int.append(ord(text[i])) i = i+1 text_crypt = [] i = 0 while i < size: text_crypt.append(int(text_int[i] ** e % n)) i = i+1 print "[ DH ] Encrypted:",text_crypt print "[ DH ] Time elapsed:",time.time()-start_time, "ms" if method == "decode": text = [eval(i) for i in raw_input('input text [decode] $ ').split()] print text start_time = time.time() size = len(text) print "[ DH ] text length:",size text_decrypt = [] i = 0 while i < size: text_decrypt.append(chr(int(text[i] ** d % n))) i = i+1 print "[ DH ] Decrypted:",text_decrypt print "[ DH ] Time elapsed:",time.time()-start_time, "ms" print "[ DH ] Type encode/decode to continue or 'exit' for exit" method = str(raw_input("$ "))
""" ------------------------------------------------------------------------ Lab 5, Task 2 - GCD ------------------------------------------------------------------------ Author: Nicolas Mills ID: 180856100 Email: [email protected] __updated__ = 2019-02-11 ------------------------------------------------------------------------ """ from functions import gcd from random import randint m = randint(0, 20000) n = randint(-20000, 20000) ans = gcd(m, n) print("m: {}\tn: {}\tans: {}".format(m, n, ans))