def fermat_factor(num): """ Implements Fermat's Factoring Algorithm. Implemented recursively because just one call will produce two factors, only one of which is prime. Uses a primality test as one of the base cases. Example: >>> fermat_factor(1723) [1723] >>> fermat_factor(1729) [13, 7, 19] """ if num < 2: return [] elif num % 2 == 0: return [2] + fermat_factor(num // 2) elif gmpy2.is_prime(num): return [num] a = gmpy2.isqrt(num) b2 = gmpy2.square(a) - num while not gmpy2.is_square(b2): a += 1 b2 = gmpy2.square(a) - num p = int(a + gmpy2.isqrt(b2)) q = int(a - gmpy2.isqrt(b2)) # Both p and q are factors of num, but neither are necessarily prime factors. # The case where p and q are prime is handled by the recursion base case. return fermat_factor(p) + fermat_factor(q)
def fermat_factors(n): assert n % 2 != 0 a = gmpy2.isqrt(n) b2 = gmpy2.square(a) - n while not gmpy2.is_square(b2): a += 1 b2 = gmpy2.square(a) - n return a + gmpy2.isqrt(b2), a - gmpy2.isqrt(b2)
def pair(x: int, y: int) -> int: assert x >= 0 assert y >= 0 if x == max(x, y): z: int = gmpy2.square(x) + x + y else: z: int = gmpy2.square(y) + x return z
def squared_distance(v, w): assert len(v) == len(w) if len(v) == 0: return gmpy2.zero() else: dist = gmpy2.square(v[0] - w[0]) for i in range(1, len(v)): dist += gmpy2.square(v[i] - w[i]) return dist
def factorise(n): #n,k=Ncheck(n) a = gmpy2.iroot(n, 2)[0] b = gmpy2.square(a) - n while ((a + b) <= n): if (gmpy2.is_square(b) == True): print(a, b) a = a + 1 b = gmpy2.iroot(gmpy2.square(a) - n, 2)[0]
def facto(n): assert n % 2 != 0 a = gmpy2.isqrt(n) b2 = gmpy2.square(a) - n while not gmpy2.is_square(b2): a += 1 b2 = gmpy2.square(a) - n p = a + gmpy2.isqrt(b2) q = a - gmpy2.isqrt(b2) return int(p), int(q)
def squared_difference(v, w): assert len(v) == len(w) if len(v) == 0: return gmpy2.zero(), () else: diff = tuple(v[i] - w[i] for i in range(len(v))) dist = gmpy2.square(diff[0]) for i in range(1, len(v)): dist += gmpy2.square(diff[i]) return dist, diff
def fermat_factors(n): assert n % 2 != 0 a = isqrt(n) b2 = square(a) - n while not is_square(b2): a += 1 b2 = square(a) - n factor1 = a + isqrt(b2) factor2 = a - isqrt(b2) return int(factor1), int(factor2)
def factor_fermat(n): assert n % 2 != 0 a = gmpy2.isqrt(n) b2 = gmpy2.square(a) - n while not gmpy2.is_square(b2): a += 1 b2 = gmpy2.square(a) - n p = a + gmpy2.isqrt(b2) q = a - gmpy2.isqrt(b2) return long(p), long(q)
def fermat_factor(n): assert n % 2 != 0 # Odd integers only a = gmpy2.isqrt(n) b2 = gmpy2.square(a) - n while not is_square(b2): a += 1 b2 = gmpy2.square(a) - n factor1 = a + gmpy2.sqrt(b2) factor2 = a - gmpy2.sqrt(b2) return int(factor1), int(factor2)
def fermat_factor(n): assert n % 2 != 0 a = gmpy2.isqrt(n) b2 = gmpy2.square(a) - n while not gmpy2.is_square(b2): a += 1 b2 = gmpy2.square(a) - n p = a + gmpy2.isqrt(b2) q = a - gmpy2.isqrt(b2)
def fermat_factor(n): assert n % 2 != 0 # Odd integers only a = gmpy2.ceil(gmpy2.sqrt(n)) b2 = gmpy2.square(a) - n while not is_square(b2): a += 1 b2 = gmpy2.square(a) - n factor1 = a + gmpy2.sqrt(b2) factor2 = a - gmpy2.sqrt(b2) return int(factor1), int(factor2)
def factor(n): assert n % 2 != 0 a = gmpy2.isqrt(n) b2 = gmpy2.square(a) - n while not gmpy2.is_square(b2): a += 1 b2 = gmpy2.square(a) - n factor1 = a + gmpy2.isqrt(b2) factor2 = a - gmpy2.isqrt(b2) return int(factor1), int(factor2)
def FermatFactor(N): A = gmpy2.mpz( gmpy2.ceil( gmpy2.sqrt(N) ) ) B2 = gmpy2.sub( gmpy2.square(A), N ) while not gmpy2.is_square(B2): A = gmpy2.add( A, gmpy2.mpz("1") ) B2 = gmpy2.sub( gmpy2.square(A), N ) B = gmpy2.sqrt(B2) P = gmpy2.mpz( gmpy2.sub( A, B ) ) Q = gmpy2.mpz( gmpy2.add( A, B ) ) if not checkFactors(P,Q,N): raise Exception("Bad factors generated") return ( P, Q )
def prime_factor(n): assert n % 2 != 0 a = isqrt(n) # làm tròn đến phần nguyên của A = căn(N) # print(a) x2 = add(square(a), -n) # x^2 = a^2 - n # print(x2) while not is_square(x2): a += 1 x2 = add(square(a), -n) p = add(a, -isqrt(x2)) q = add(a, isqrt(x2)) return int(p), int(q)
def fermat_factor(n): assert n % 2 != 0 a = gmpy2.isqrt(n) b2 = gmpy2.square(a) - n for i in range(100000000): a += 1 b2 = gmpy2.square(a) - n if gmpy2.is_square(b2): p = a + gmpy2.isqrt(b2) q = a - gmpy2.isqrt(b2) return True, (int(p), int(q)) return False, ()
def riesz_force(points, s): point_indices = range(len(points)) dim_indices = range(len(points[0])) force = [[gmpy2.zero() for _ in dim_indices] for _ in point_indices] if s == 2: for i in point_indices: for j in point_indices: if i != j: dist, disp = squared_difference(points[i], points[j]) dist = 2 / gmpy2.square(dist) for k in dim_indices: force[i][k] += dist * disp[k] elif s == 1: for i in point_indices: for j in point_indices: if i != j: dist, disp = squared_difference(points[i], points[j]) dist = gmpy2.rec_sqrt(dist) / dist for k in dim_indices: force[i][k] += dist * disp[k] else: for i in point_indices: for j in point_indices: if i != j: dist, disp = squared_difference(points[i], points[j]) dist = s * dist**(-s / 2 - 1) for k in dim_indices: force[i][k] += dist * disp[k] return force
def fermat_factor(n): assert n % 2 != 0 a = gmpy2.isqrt(n) b2 = gmpy2.square(a) - n while not gmpy2.is_square(b2): a += 1 b2 = gmpy2.square(a) - n p = a + gmpy2.isqrt(b2) q = a - gmpy2.isqrt(b2) print("p = {}".format(p)) print("q = {}".format(q)) return int(p), int(q)
def is_square(n): n = gmpy2.mpz(n) root = gmpy2.isqrt(n) if gmpy2.square(root) == n: return root else: return None
def fermat_factor(n): assert n % 2 != 0 a = gmpy2.isqrt(n) b2 = gmpy2.square(a) - n print("[+] Working...") while not gmpy2.is_square(b2): a += 1 print("[x] a = {}".format(a)) b2 = gmpy2.square(a) - n p = a + gmpy2.isqrt(b2) q = a - gmpy2.isqrt(b2) return int(p), int(q)
def fermat_factors(n): """ Factor given number using Fermat approach, starting from sqrt(n) :param n: modulus to factor :return: p, q """ assert n % 2 != 0 import gmpy2 a = gmpy2.isqrt(n) b2 = gmpy2.square(a) - n while not gmpy2.is_square(b2): a += 1 b2 = gmpy2.square(a) - n factor1 = a + gmpy2.isqrt(b2) factor2 = a - gmpy2.isqrt(b2) print(n, factor1, factor2) return int(factor1), int(factor2)
def unpair(z: int) -> (int, int): assert z >= 0 x: int = z - gmpy2.square(gmpy2.isqrt(z)) w: int = gmpy2.isqrt(z) if x < w: return x, w else: y: int = x - w return w, y
def reduced_isometric(p, q, symmetry_group, epsilon=1e-10): found = [False for _ in range(len(q))] for j, dist in reduced_nearest_neighbor_indices(p, q, symmetry_group): if dist > gmpy2.square(epsilon): return False if found[j]: return False found[j] = True return all(found)
def populate(limit): for n in itertools.count(1): if n > limit: break if not is_palindrome(n): continue square = gmpy2.square(n) if square > limit: break if is_palindrome(square): square_palindromes.add(square)
def fermat_factor(n): assert n % 2 != 0 a = gmpy2.isqrt(n) b2 = gmpy2.square(a) - n for i in range(100000000): a += 1 b2 = gmpy2.square(a) - n if gmpy2.is_square(b2): p = a + gmpy2.isqrt(b2) q = a - gmpy2.isqrt(b2) return True, (int(p), int(q)) return False, () # # print(fermat_factor(0x4e733febb94db17ca3e6aa26ec33b4960c150c52300e06c60b3318f0744fef2d687a8f5bf598894a22eec4abdae01b197e4cc5603de67eb670e261eb4e4cc5e26241edcde494cce415bbc5a410abcefdff6199bbcdf62e9d434faa88a1d16012520f80d126208206ff80191e20ed7423cdce5b8a555b4161534e789a74f0a701))
def find_sqrt_ceil(N): lower_bound = mpz(2) upper_bound = N while True: times_two = mul(lower_bound, 2) if square(times_two) > N: upper_bound = times_two break else: lower_bound = times_two while True: middle = div(lower_bound + upper_bound, 2) if square(middle) == N: return middle elif square(middle) < N and square(middle + 1) > N: return middle + 1 elif square(middle) < N: lower_bound = middle else: upper_bound = middle
def question3(): N = gmpy2.mul( gmpy2.mpz( "720062263747350425279564435525583738338084451473999841826653057981916355690188337790423408664187663938485175264994017897083524079135686877441155132015188279331812309091996246361896836573643119174094961348524639707885238799396839230364676670221627018353299443241192173812729276147530748597302192751375739387929" ), 6) A = gmpy2.ceil(gmpy2.sqrt(N)) #A-N^(1/6)<neg k = gmpy2.sub(gmpy2.square(A), N) print(k) x = gmpy2.ceil(gmpy2.sqrt( k)) #same trick to calculate the other half to recover p,q (3p-2q)/2 print("x:", x) p = (A - x) / 2 q = (A + x) / 3 print("p:", p, "q:", q)
def exec_pollard_rho(n): # Find prime factor of n x = random.randrange(1, n, 2) i, k = 1, 2 y = x while True: i += 1 x2 = (gmpy2.square(x) - 1) % n p = gmpy2.gcd(y - x2, n) if (p != 1) and (p != n): break x = x2 if i == k: y = x2 k *= 2 return p
def prime_factor(n): assert n % 2 != 0 a = isqrt(24 * n) + 1 # làm tròn đến phần nguyên của A = căn(6N) # print(a) x2 = add(square(a), -24 * n) # x^2 = a^2 - n # print(x2) ''' while not is_square(x2): a += 1 x2 = square(a) - 24*n ''' p = div(a - isqrt(x2), 6) q = div(a + isqrt(x2), 4) return int(p), int(q)
def fermat_factorization(n): if n <= 3: return [n] if n % 2 == 0: return [2] + fermat_factorization(n//2) a = gmpy2.isqrt(n) if a * a == n: return [int(a), int(a)] else: a += 1 b2 = gmpy2.square(a) - n while not gmpy2.is_square(b2): a += 1 b2 = a*a - n b = gmpy2.isqrt(b2) return [int(k) for results in (fermat_factorization(i) for i in [a - b, a + b] if 1 < i < n) for k in results] or [int(n)]
def question1(): N = gmpy2.mpfr( "179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581", 1050) #print("N",N) A = gmpy2.ceil((gmpy2.sqrt(N))) #print("A:",A) k = gmpy2.square(A) - N #print("K:",k) x = gmpy2.ceil(gmpy2.sqrt(k)) print("x:", x) p = A + x q = A - x if (N == gmpy2.mul(p, q)): if (p > q): p, q = q, p print("found pq:", p, q)
def question2(): #25464796146996183438008816563973942229341454268524157846328581927885777969985222835143851073249573454107384461557193173304497244814071505790566593206419759 N = gmpy2.mpfr( "648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877", 1050) A = gmpy2.ceil((gmpy2.sqrt(N))) #print("A:",A) i = 1 while i < 2**20: #print("K:",k) x = gmpy2.ceil(gmpy2.sqrt(gmpy2.square(A) - N)) #print("x:",x) p = A + x q = A - x if (N == gmpy2.mul(p, q)): if (p > q): p, q = q, p print("found pq:", p, q) break A += 1 i += 1
def linear_cong_rng(): x[0] = 56687054115473550533 x[1] = 71501923691929981066 x[2] = 1162551557687152936 x[3] = 88117163952857350660 x[4] = 16754986973331962115 t[0] = gmpy2.sub(x[1], x[0]) t[1] = gmpy2.sub(x[2], x[1]) t[2] = gmpy2.sub(x[3], x[2]) t[3] = gmpy2.sub(x[4], x[3]) u[0] = abs(gmpy2.mul(t[2], t[0]) - gmpy2.square(t[1])) u[1] = abs(gmpy2.mul(t[3], t[1]) - gmpy2.square(t[2])) # M' gcd = gmpy2.gcd(u[0], u[1]) # true M = M'/2 M = gmpy2.mpz(gmpy2.div(gcd, 2)) # x[2] = A * x[1] + B (mod M) # x[1] = A * x[0] + B (mod M) # --------------------------- # x[2] - x[1] = A (x[1] - x[0]) mod M # t[1] = A * t[0] mod M # A * t[0] = t[1] mod M # -x[0] * x[2] = -A * x[0] * x[1] - B * x[0] (mod M) # x[1] * x[1] = A * x[0] * x[1] + B * x[1] (mod M) # -------------------------------------------------- # x[1]^2 - (x[0] * x[2]) = B (x[1] - x[0]) mod M # B * t[0] = (x[1]^2 - (x[0] * x[2])) mod M # easier way to solve B: # find A first # (A * x[0] + B) = x[1] mod M # used Diophantine equations to retrieve A and B # using M #A = 12630192673789351314 #B = 35234390061212433526 # using M = M'/2 or M'/3 A = 12630192673789351314 B = 35234390061212433526 print "t[0] = %i\nt[1] = %i\nt[2] = %i\nt[3] = %i\n\nu[0] = %i\nu[1] = %i\n\nM' = gcd(u[0], u[1]) = %i\nM = M'/2 = %i\n" %(t[0], t[1], t[2], t[3], u[0], u[1], gcd, M) x[5] = gmpy2.mpz(gmpy2.f_mod(gmpy2.add(gmpy2.mul(A, x[4]), gmpy2.mpz(B)), gmpy2.mpz(M))) x[6] = gmpy2.mpz(gmpy2.f_mod(gmpy2.add(gmpy2.mul(A, x[5]), gmpy2.mpz(B)), gmpy2.mpz(M))) x[7] = gmpy2.mpz(gmpy2.f_mod(gmpy2.add(gmpy2.mul(A, x[6]), gmpy2.mpz(B)), gmpy2.mpz(M))) x[8] = gmpy2.mpz(gmpy2.f_mod(gmpy2.add(gmpy2.mul(A, x[7]), gmpy2.mpz(B)), gmpy2.mpz(M))) x[9] = gmpy2.mpz(gmpy2.f_mod(gmpy2.add(gmpy2.mul(A, x[8]), gmpy2.mpz(B)), gmpy2.mpz(M))) x[10] = gmpy2.mpz(gmpy2.f_mod(gmpy2.add(gmpy2.mul(A, x[9]), gmpy2.mpz(B)), gmpy2.mpz(M))) x[11] = gmpy2.mpz(gmpy2.f_mod(gmpy2.add(gmpy2.mul(A, x[10]), gmpy2.mpz(B)), gmpy2.mpz(M))) #print "x[5] = ", x[5] #print "x[6] = ", x[6] #print "x[7] = ", x[7] #print "x[8] = ", x[8] #print "x[9] = ", x[9] print "x[10] = ", x[10] print "x[11] = ", x[11] print "A = ", A print "B = ", B return
import gmpy2 from gmpy2 import mpz __author__ = 'Qubo' N1 = '17976931348623159077293051907890247336179769789423065727343008115' N1 += '77326758055056206869853794492129829595855013875371640157101398586' N1 += '47833778606925583497541085196591615128057575940752635007475935288' N1 += '71082364994994077189561705436114947486504671101510156394068052754' N1 += '0071584560878577663743040086340742855278549092581' gmpy2.set_context(gmpy2.context(precision=2000)) N = mpz(N1) A = gmpy2.ceil(gmpy2.sqrt(N)) SquareA = gmpy2.square(A) if gmpy2.is_square(mpz(gmpy2.sub(SquareA, N))): x = gmpy2.sqrt(gmpy2.sub(SquareA, N)) print 'p = ' + str(mpz(gmpy2.sub(A, x))) print 'q = ' + str(mpz(gmpy2.add(A, x))) else: print 'x is not square, must be wrong...'
def square(self): return Numeric(gmpy2.square(self.val))
from gmpy2 import isqrt, square, invert, powmod def printPQ(N, p, q): if not N == p * q: return print min(p, q) print print "QUESTION 1" n1 = 179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581 a1 = isqrt(n1) + 1 x1 = isqrt(square(a1) - n1) printPQ(n1, a1 - x1, a1 + x1) print "QUESTION 2" n2 = 648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877 a2, x2 = isqrt(n2), 0 while (a2 - x2) * (a2 + x2) != n2: a2 = a2 + 1 x2 = isqrt(square(a2) - n2) printPQ(n2, a2 - x2, a2 + x2) def solveQuadratic(a, b, c): r = isqrt(square(b) - 4 * a * c) return ((-b + r) / (2 * a), (-b - r) / (2 * a)) print "QUESTION 3"
def solveQuadratic(a, b, c): r = isqrt(square(b) - 4 * a * c) return ((-b + r) / (2 * a), (-b - r) / (2 * a))