def init2(self): f = self.length_sq() self.rational = gmpy2.is_square(f.numerator) and gmpy2.is_square(f.denominator) if self.rational: n = int(gmpy2.iroot(f.numerator, 2)[0]) d = int(gmpy2.iroot(f.denominator, 2)[0]) self.length = fractions.Fraction(n, d)
def sqrt(x): p = x.rational_part s, t = p.numerator, p.denominator if p == 0: return Quadratic() elif p < 0: raise NotImplementedError elif x.quadratic_power == 0: if is_square(s) and is_square(t): return Quadratic(Fraction(isqrt(s), isqrt(t))) r = Fraction(1, t) p = s * t prime_power_list = [] for prime, x_power in zip(primes, x.quadratic_part or (0, ) * len(primes)): power = 0 while p % prime == 0: p //= prime power += 1 r *= prime**(power >> 1) prime_power_list.append(((power & 1) << x.quadratic_power) | x_power) if not is_square(p): return return Quadratic(r * isqrt(p), x.quadratic_power + 1, tuple(prime_power_list))
def fermat_factor(n): """ Fermat's factorization method Args: n (int): target Returns: int: factor References: - https://www.geeksforgeeks.org/fermats-factorization-method - https://en.wikipedia.org/wiki/Fermat%27s_factorization_method """ if gmpy2.is_square(n): tmp = int(gmpy2.isqrt(n)) return tmp a = gmpy2.isqrt(n) b = a**2 - n while not gmpy2.is_square(b): b += 2 * a + 1 a += 1 b_sqrt = gmpy2.isqrt(b) return int(a - b_sqrt)
def Euler_98(i_file='098.txt'): '''Find the largest square anagram in the given text file The anagram of the word must exist in the text file ''' with open(i_file, encoding="utf-8") as data: for a_line in data: word_tuple = tuple([x.strip('"') for x in a_line.split('","')]) if not word_tuple: return None word_list = [x for x in reversed(sorted(list(word_tuple), key=len))] dict_list = [{a:x.count(a) for a in x} for x in word_list] ''' word_tuple = tuple(word_list) for x in range(len(word_tuple)): pair = False for y in range(len(word_list)): if (not word_tuple[x] == word_list[y]) and dict_list[ print(x,y) pair = True break if not pair: word_list.remove(x) ''' print(word_list) word_tuple = tuple(word_list) max_length = 0 max_sq = 0 digits = tuple(ord(c) for c in '0123456789') for index in range(word_tuple): word = word_tuple[index] tmp = dict_list[index] dict_list[index] = 0 if not tmp in dict_list: break if len(word) < max_length: break letter_set = tuple(ord(c) for c in set(word)) for guess in itertools.permutations(digits, len(letter_set)): translation = dict(zip(letter_set, guess)) if not gmpy2.is_square(int(word.translate(translation))): continue word_dict = {x:word.count(x) for x in word} for candidate in word_tuple: if word_dict == {x:candidate.count(x) for x in candidate} and gmpy2.is_square(int(candidate.translate(translation))): if int(candidate.translate(translation)) > max_sq: max_sq = int(candidate.translate(translation)) max_length = len(candidate) if int(word.translate(translation)) > max_sq: max_sq = int(word.translate(translation)) max_length = len(word) return max_sq
def fermat(n): assert n % 2 == 1 if is_square(n) == 1: return isqrt(n) isqrtn = isqrt(n) b2 = (isqrtn + 1)**2 - n for a in range(isqrtn + 1, (n + 9)//6 + 2): if is_square(b2): return a - isqrt(b2) b2 = b2 + 2*a + 1 return -1 # We have a prime number
def main(): ''' Driver function ''' valid = [] base1, base2 = 7, 11 for n in range(2, 10**7): inc1 = 6 * n + 1 inc2 = 6 * n + 5 if is_square(base1) and check_area(n + 1, base1): valid.append((n, n + 1)) if is_square(base2) and check_area(n - 1, base2): valid.append((n, n - 1)) base1 += inc1 base2 += inc2 print(valid)
def fermatFactor(n): a = gmpy2.iroot(gmpy2.mpz(n), 2)[0] + 1 b2 = a**2 - n while gmpy2.is_square(b2) is False: a += 1 b2 = a**2 - n return (a - gmpy2.iroot(b2, 2)[0], a + gmpy2.iroot(b2, 2)[0])
def p80(): S = 0 for n in range(0, 101): if gmpy2.is_square(n) == False: s = getS(n) S += sum([int(n) for n in s]) return S
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 isCleanMatch(v1, v2, n): if len(v1) != len(str(n)) or len(v2) != len(str(n)): return False map = {} inverseMap = {} for s in reversed(v1): if ((s in map and map[s] != n % 10) or (n % 10 in inverseMap and s not in map)): return False inverseMap[n % 10] = 0 map[s] = n % 10 n //= 10 temp = 0 if (map[v2[0]] == 0): return False for s in v2: temp *= 10 temp += map[s] return gmpy2.is_square(temp)
def wieners_attack(e: int, n: int) -> Optional[int]: """Wiener's Attack Args: e (long): RSA public key e n (long): RSA public key n Returns: Optional[int]: RSA private key Examples: >>> wieners_attack(2621, 8927) 5 Refs: - https://github.com/orisano/owiener - https://github.com/pablocelayes/rsa-wiener-attack - http://www.reverse-engineering.info/Cryptography/ShortSecretExponents.pdf """ convergents = convergents_from_contfrac(to_contfrac(e, n)) for k, dg in convergents: edg = e * dg phi = edg // k x = n - phi + 1 # (n - phi + 1) // 2 is integer and ((n - phi + 1) // 2)^2 - pq is perfect square if x % 2 == 0 and gmpy2.is_square(pow(x // 2, 2) - n): return dg // (edg % k) return None
def _search_step_i(self, i): for j in range(len(self.R)): res, r = self._search_step_j(i, j) r = self.h * (self.k0 + i) + self.R[j] return is_square(r**2 - self._4n), r if res: return res, (j, r) return False, None
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 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 minx(D): x = isqrt(D) zero = mpz(0) while 1: ysq, rem = t_divmod(x*x - 1,D) if rem == zero and is_square(ysq): print(x, isqrt(ysq)) return x x += 1
def fair_square_numbers_in_range(lower, upper): count = 0 for i in my_range(lower, upper + 1): #print i, int(gmpy2.is_square(i)), int(is_palindrome(i)), int(is_palindrome(int(round(gmpy2.sqrt(i))))) if gmpy2.is_square(i) and is_palindrome(i) and is_palindrome( int(round(gmpy2.sqrt(i)))): #print i count += 1 return count
def find_xyz(): n = 3 while True: for p in range(2, n): if not gmpy2.is_square(n ** 2 - p ** 2): continue for m in range(1, p): if p ** 2 <= (n ** 2 + m ** 2) / 2: break if int(math.fabs(n - m)) % 2 == 1: continue nmp = n ** 2 + m ** 2 - p ** 2 if gmpy2.is_square(nmp) and gmpy2.is_square(p ** 2 - m ** 2): x = (n ** 2 + m ** 2) // 2 y = (n ** 2 - m ** 2) // 2 z = p ** 2 - x return x+y+z n += 1
def wieners_attack(e: int, n: int) -> Optional[int]: for k, dg in conv_from_cfrac(rat_to_cfrac(e, n)): edg = e * dg phi = edg // k x = n - phi + 1 if x % 2 == 0 and is_square((x // 2)**2 - n): g = edg - phi * k return dg // g return None
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_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 fermat(n): a = int(gmpy2.ceil(gmpy2.isqrt(n))) b2 = (a**2) - n step = 0 while not gmpy2.is_square(b2): a += 1 b2 = (a**2) - n step += 1 print(n, step, a, b2) return a - gmpy2.isqrt(b2), a + gmpy2.isqrt(b2)
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 factor(n): avg_guess = gmpy2.isqrt(n) + (0 if gmpy2.is_square(n) else 1) while True: x = gmpy2.isqrt(avg_guess ** 2 - n) p = avg_guess - x q = avg_guess + x if (p * q == n): break else: avg_guess += 1 return (p, q)
def factor(n, k=1): nk_sqrt = isqrt(n * k) for i in range(1, 2**20): a = nk_sqrt + i t = a * a - n * k if is_square(t): x = isqrt(t) p = gcd(n, a - x) q = gcd(n, a + x) if mul(p, q) == n: return [p, q]
def calcA(a,n): x=a**2-n if gmpy2.is_square(x): x = gmpy2.isqrt(x) p = a+x q = a-x if p*q == n: if not gmpy2.is_prime(q): print "not prime q" # simple check return q return 0
def k_t_from_pprime_qprime_l_alpha_beta(pprime, qprime, l, alpha, beta): a = pprime - l b = -beta c = alpha * (qprime - l) disc = b * b - 4 * a * c assert gmpy2.is_square(disc) temp = -b + gmpy2.isqrt(disc) assert temp % (2*a) == 0 k = temp // (2*a) assert alpha % k == 0 return k, alpha // k
def calcA(a, n): x = a**2 - n if gmpy2.is_square(x): x = gmpy2.isqrt(x) p = a + x q = a - x if p * q == n: if not gmpy2.is_prime(q): print "not prime q" # simple check return q return 0
def minxfromy(D): y = 1 ysq = y*y while 1: xsq = 1 + D*ysq if is_square(xsq): x = isqrt(xsq) return x ysq += 2*y + 1 y += 1
def getFactorCount(n): count = 0 for i in range(1, math.floor(math.sqrt(n)) + 1): if (n % i == 0): count += 2 if (gmpy2.is_square(n)): count -= 1 return count
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 pq(n): B=math.factorial(2**14) u=0;v=0;i=0 u0=gmpy2.iroot(n,2)[0]+1 while(i<=(B-1)): u=(u0+i)*(u0+i)-n if gmpy2.is_square(u): v=gmpy2.isqrt(u) break i=i+1 p=u0+i+v return p
def fermat_factors(n): """ use Fermat's theroem to decompose {n} into two prime numbers i.e., to crack the RSA key by factorizing {n} """ Number = mpz(n) if gmpy2.is_square(Number): a = gmpy2.isqrt(Number) return (a, a) a = gmpy2.isqrt(Number) + 1 while True: b2 = gmpy2.mul(a, a) - Number if gmpy2.is_square(b2): break else: a += 1 b = gmpy2.isqrt(b2) return (a - b, a + b)
def default_keylists(*args, **kwargs): """Default arg for build_config""" size = kwargs.pop('size', None) if size is None: size = 9 assert gmpy2.is_square(size), "size must be a square number" groupwidth = int(gmpy2.sqrt(size)) k1 = lambda i, j: i // groupwidth + (j // groupwidth) * groupwidth k2 = lambda i, j: i % groupwidth + (j % groupwidth) * groupwidth return [ [(k1(i,j),k2(i,j)) for i in range(size)] for j in range(size) ]
def testdice(facesa, facesb): seta, setb = set(facesa), set(facesb) if 6 in facesa or 9 in facesa: seta |= {6, 9} if 6 in facesb or 9 in facesb: setb |= {6, 9} numbers = set() for faceA in seta: for faceB in setb: numbers.add(10 * faceA + faceB) numbers.add(10 * faceB + faceA) squares = [num for num in numbers if num > 0 and is_square(num)] return len(squares) == 9
def fermat(n): a = isqrt(n) + 1 while not is_square(pow(a, 2) - n): a += 1 b = isqrt(pow(a, 2) - n) p = a + b q = a - b assert p * q == n return p, q
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 solve_quadratic_eq(a, b, c): #solve a*x^2+bx+c=0 (integer coeff) d = b * b - 4 * a * c sols = [] if d == 0: sols.append(-b // 2 // a) elif d > 0: print('FUUU', b) print(gmpy2.is_square(d)) dd = gmpy2.isqrt(d) sols.append((-b - dd) // (2 * a)) sols.append((-b + dd) // (2 * a)) return sols
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 fermat_factoring(N, trial = 1 << 32): """Perform Fermat's factorization. :param N: number to factorize. :param trial: (optional) maximum trial number. """ x = gmpy2.isqrt(N) + 1 y = x * x - N for i in xrange(trial): if gmpy2.is_square(y): y = gmpy2.isqrt(y) return (x + y,x - y) y += (x << 1) + 1 x += 1 # Failed return None
def qs3(): n = mpz('720062263747350425279564435525583738338084451473999841826653057981916355690188337790423408664187663938485175264994017897083524079135686877441155132015188279331812309091996246361896836573643119174094961348524639707885238799396839230364676670221627018353299443241192173812729276147530748597302192751375739387929') a = gmpy2.isqrt(4*6*n)+1 # 2A x = a**2-6*n*4 if gmpy2.is_square(x): x = gmpy2.isqrt(x) p = (a-x)/6 q = (a+x)/4 if p*q == n: if not gmpy2.is_prime(q): print "not prime q" # simple check if p>q: return q else: return p return 0
def weiner(N,e): for c in cf2cvg(f2cf(e,N)): k = c.numerator if k == 0: continue d = c.denominator phi = (e*d - 1) / k b = N - phi + 1 det = b*b - 4*N if det < 0: continue root = g.mpz(g.sqrt(det)) if g.is_square(det) and g.is_even(b + root): p = (b + root) / 2 q = (b - root) / 2 if checkFactors(p,q,N): return (p,q,d) raise Exception("Invalid result generated")
def factor2(n): avg_guess = gmpy2.isqrt(6 * n) + (0 if gmpy2.is_square(n) else 1) while True: a = gmpy2.mpz(1) b = gmpy2.mpz(1) c = 6 * n - avg_guess ** 2 + avg_guess x_roots = quadform(a, b, c) for x in x_roots: # Check for 3p < 2q p = gmpy2.div((avg_guess - x - 1), 3) q = gmpy2.div((avg_guess + x), 2) if (p * q == n): return (p, q) # Check for 3p > 2q p = gmpy2.div((avg_guess + x), 3) q = gmpy2.div((avg_guess - x - 1), 2) if (p * q == n): return (p, q) avg_guess += 1
# Small case 5 # p = mpz('56374829353') # g = mpz('34785642345') # y = mpz('9428991196') # time taken: 1hr + # x = # get m, ceiling of sqrt(p) m = gmpy2.isqrt(p) # m = gmpy2.isqrt(m) # m = gmpy2.isqrt(m) # m = gmpy2.isqrt(m) # m = gmpy2.isqrt(m) if not gmpy2.is_square(p): m += 1 invgm = gmpy2.powmod(g, -m, p) # to track progress pj = mpz("0") # pi = mpz('0') # set up left hand side of comparison i = 0 leftArray = [] # item 0, i = 0 # leftArray.append(mpz('1')) # item 1, i = 1 # leftArray.append(mpz(g.digits(10)))
def isPerfectRoot(n): return gmpy2.is_square(n)
import math import numpy as np import gmpy2 import time now = time.time() n_list = [] limit = 10 ** 12 for a in range (1, 10 ** 4): for b in range (1, a): if a ** 3 * b + b ** 2 >= limit: break if math.gcd(a,b) > 1: continue k = 1 while True: n = a ** 3 * k ** 2 * b + k * b ** 2 if n >= limit: break if gmpy2.is_square(n): n_list.append(n) k += 1 print (sum(n_list)) print ('time spent is {}'.format(time.time() - now))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #Title: Arranged probability import math import sys from gmpy2 import is_square from decimal import * n = 10 ** 12 accuracyRange = 0.1 bi = 15 factor = Decimal(1154) ** Decimal(1.0 / 4.0) while True: guess = int(Decimal(bi) * factor) for i in range(int(guess * (1 - accuracyRange)), int(guess * (1 + accuracyRange))): c = 2 * i * (i - 1) square = c + int(math.sqrt(c) + 1) if is_square(square): x = math.sqrt(2 * i * (i - 1) + 1/4) + 1/2 accuracyRange = abs(float(Decimal(i - guess)/ Decimal(i))) if x > n: print(i) sys.exit(0) bi = i break
# small case 1 # p = 1049, q = 1103 # n = mpz('1157047') # small case 2 # p = 1459153, q = 1459583 # n = mpz('2129754913199') # small case 3 # p = 169743212304 q = 169743214699 n = mpz('28812758529815810456496') m = n # ceil(x^(1/2)) if gmpy2.is_square(m): m1 = gmpy2.isqrt(m) else: m1 = gmpy2.isqrt(m) m1 += 1 print "m1= " print m1.digits(10) # ceil(x^(1/4)) if gmpy2.is_square(m1): m2 = gmpy2.isqrt(m1) else: m2 = gmpy2.isqrt(m1) m += 1
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...'
# Program that find prime factors iff next (perfect square - N) is a perfect square import gmpy2 import sys from gmpy2 import mpz n = mpz('179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581') # n = mpz('3405971539559') if gmpy2.is_square(n): sqrt = gmpy2.isqrt(n) else: sqrt = gmpy2.isqrt(n) sqrt += 1 nsquare = gmpy2.mul(sqrt, sqrt) print "Next perfect square:\n" + nsquare.digits(10) diff = n - nsquare if diff < 0: diff = -diff print "Diff:\n" + diff.digits(10) if gmpy2.is_square(diff): print "Diff is a perfect square" else: print "Diff is not a perfect square" sys.exit()
import math import numpy as np import gmpy2 import time LIMIT = 100000000 abc_list = [] multiply = 1 prev_a = 3 / 7 a = 3 while True: c_square = a ** 2 + (a + 1) ** 2 if gmpy2.is_square(c_square): multiply = a / prev_a c = int(math.sqrt(c_square)) abc = 2*a+1+c if abc < LIMIT: abc_list.append(abc) else: break print(a, 2*a+1+c, multiply) prev_a = a a = int(a * multiply) if a >= LIMIT: break else: a -= 1 print (sum(LIMIT // p for p in abc_list))
import gmpy2 from gmpy2 import mpz from gmpy2 import mpfr gmpy2.get_context().precision = 2000 N1 = mpz("179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581") A1 = gmpy2.isqrt(N1) + 1 #A = (p+q/2) and is close to sqrt(N), so sqrt(N) is used to estimate A x1 = gmpy2.isqrt(A1**2 - N1) p1 = A1 - x1 q1 = A1 + x1 print p1 N2 = mpz("648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877") A2 = gmpy2.isqrt(N2) + 1 while not gmpy2.is_square(A2**2 - N2): A2 = A2 + 1 print A2 - gmpy2.isqrt(A2**2 - N2) N3 = mpz("720062263747350425279564435525583738338084451473999841826653057981916355690188337790423408664187663938485175264994017897083524079135686877441155132015188279331812309091996246361896836573643119174094961348524639707885238799396839230364676670221627018353299443241192173812729276147530748597302192751375739387929") A3 = gmpy2.ceil(gmpy2.sqrt(6*N3)*2)/2 print (A3 - gmpy2.sqrt(A3**2 - 6*N3))/3 e_m = mpz("22096451867410381776306561134883418017410069787892831071731839143676135600120538004282329650473509424343946219751512256465839967942889460764542040581564748988013734864120452325229320176487916666402997509188729971690526083222067771600019329260870009579993724077458967773697817571267229951148662959627934791540") phi = (p1 - 1)*(q1 - 1) d = gmpy2.invert(65537, phi) d_m = gmpy2.powmod(e_m, d, N1) hex = d_m.digits(16) hex = hex[hex.find("00")+2:] print hex.decode("hex")
#get it from CTF2015 Weak RSA #use to split q and p #from https://github.com/ctfs/write-ups-2015/tree/master/pragyan-ctf-2015/crypto/weak_rsa import math import gmpy2 N=gmpy2.mpz(E8953849F11E932E9127AF35E1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000051F8EB7D0556E09FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBAD55) gmpy2.get_context().precision=2048 a=int(gmpy2.sqrt(n)) a2=a*a b2=gmpy2.sub(a2,N) while not(gmpy2.is_square(b2)): a=a+1 b2=a*a-N b2=gmpy2.mpz(b2) gmpy2.get_context().precision=2048 b=int(gmpy2.sqrt(b2)) print a+b print a-b
print("first factoring challenge") print("p=",p) # second assignment N=mpz(648455842808071669662824265346772278726343720706976263060439070378797308618081116462714015276061417569195587321840254520655424906719892428844841839353281972988531310511738648965962582821502504990264452100885281673303711142296421027840289307657458645233683357077834689715838646088239640236866252211790085787877) A = gmpy2.isqrt(N) while True: A += 1 tmp = A*A-N if not gmpy2.is_square(tmp): continue x = gmpy2.isqrt(tmp) p = A - x q = A + x if p*q == N: print("second factoring challenge") print("p=",p) break # third assignment N=720062263747350425279564435525583738338084451473999841826653057981916355690188337790423408664187663938485175264994017897083524079135686877441155132015188279331812309091996246361896836573643119174094961348524639707885238799396839230364676670221627018353299443241192173812729276147530748597302192751375739387929
def isSquare(a): return gmpy2.is_square(a)
import time now = time.time() def x(n): return 5 * n ** 2 + 14 * n + 1 multiply_odd = 2.5 multiply_even = 4.2 prev_i = 1 i = 2 multiply = 0 golden_nuggets = [] while len(golden_nuggets) < 30: xi = x(i) if gmpy2.is_square(xi): temp_i = i print (temp_i, xi, temp_i / prev_i) golden_nuggets.append(temp_i) if len(golden_nuggets) % 2 == 0: i = int(temp_i * multiply_even) multiply_odd = temp_i / prev_i else: i = int(temp_i * multiply_odd) if prev_i > 1: multiply_even = temp_i / prev_i prev_i = temp_i else: i -= 1 print (sum(golden_nuggets))
def argminxD(D): return max((mpz(d) for d in range(1, D + 1) if not is_square(d)), key=minxfromy)