def solve(): N = 2461799993978700679 primelist = primes(10000) roots, rowlist = find_candidates(N, primelist) M = echelon.transformation_rows(rowlist) for v in reversed(M): a, b = find_a_and_b(v, roots, N) if gcd(a - b, N) != 1: return gcd(a - b, N)
def solve(): N = 2461799993978700679 primelist = primes(10000) roots, rowlist = find_candidates(N, primelist) M = echelon.transformation_rows(rowlist) for v in reversed(M): a,b = find_a_and_b(v, roots, N) if gcd(a-b,N) != 1: return gcd(a-b,N)
def find(): N=2461799993978700679 primelist=primes(10000) (roots, rowlist) = find_candidates(N, primelist) M=echelon.transformation_rows(rowlist) counter=1 while True: v=M[-1*counter] counter = counter+1 (a,b)=find_a_and_b(v, roots, N) print(gcd(a-b, N)) if gcd(a-b, N)>1: break;
def checkEuclid(r, s, t): a = r * s b = s * t d = gcd(a, b) print('gcd of %s and %s is %s' % (a, b, d)) print('%s/%s=%s, %s/%s==%s' % (a, d, a / d, b, d, b / d)) print('d=%s, s=%s' % (d, s))
def factor(N): ret = [] print('N=', N) primeset = primes(10000) primelist = list(primeset) #shuffle(primelist) primelist.sort(reverse=True) roots, rowlist = find_candidates(N, primeset) M = echelon.transformation_rows(rowlist) for i in reversed(range(len(M))): a, b = find_a_and_b(M[i], roots, N) if gcd(a-b, N) != 1: print('find ', gcd(a-b, N)) ret.append(gcd(a-b, N)) N //= gcd(a-b, N) if N == 1: break return ret
def factor_woojoo(N, primeset): roots, rowlist = find_candidates(N, primeset) M = echelon.transformation_rows(rowlist, sorted(primeset, reverse=True)) rank = independence.rank(rowlist) for i in range(len(M) - 1, rank - 1, -1): a, b = find_a_and_b(M[i], roots, N) ans = gcd(a - b, N) if ans > 1: return ans return None
def get_gcd(N, primeset): denom = 1 cands = find_candidates(N, primeset) M = transformation_rows(cands[1], sorted(primeset, reverse=True)) M_index = len(M)-1 while denom == 1 and M_index > 0: v = M[M_index] (aint, bint) = find_a_and_b(v, cands[0], N) denom = gcd(aint-bint, N) M_index -= 1 return denom
def solve(N, primelist): roots, rowlist = find_candidates(N, primelist) M = echelon.transformation_rows(rowlist) index = -1 while True: v = M[index:][0] a, b = find_a_and_b(v, roots, N) g = gcd(a - b, N) if g != 1: return g else: index = index - 1
def find_nontrivial_divisor(M,rowlistMat,roots,N): smallV = float("inf") lenM = len(M) zeroV = [] for i in range(0,lenM): if (M[lenM-i-1]) * rowlistMat == Vec(rowlistMat.D[1],{}): (a,b) = find_a_and_b(M[lenM-i-1],roots,N) divisor = gcd(a-b,N) if divisor < smallV and divisor != 1 and divisor != N: smallV = divisor else: break return smallV
def find_non_trivial_div(N): pr_lst = primes(10000) roots, rowlist = find_candidates(N, pr_lst) M = echelon.transformation_rows(rowlist, sorted(pr_lst, reverse=True)) A = rowdict2mat(rowlist) for v in reversed(M): if not is_zero_vec(v*A): raise Exception("Unable to find non-trivial div") a, b = find_a_and_b(v, roots, N) div_candidate = gcd(a - b, N) if div_candidate != 1: return div_candidate raise Exception("Unable to find non-trivial div")
def find_nontrivial_divisor(M, rowlistMat, roots, N): smallV = float("inf") lenM = len(M) zeroV = [] for i in range(0, lenM): if (M[lenM - i - 1]) * rowlistMat == Vec(rowlistMat.D[1], {}): (a, b) = find_a_and_b(M[lenM - i - 1], roots, N) divisor = gcd(a - b, N) if divisor < smallV and divisor != 1 and divisor != N: smallV = divisor else: break return smallV
def task5(N, p): primelist = primes(p) #N = 2461799993978700679 roots, rowlist = find_candidates(N, primelist) M = echelon.transformation_rows(rowlist) #print(M) for r in reversed(range(len(M))): v = M[r] a, b = find_a_and_b(v, roots, N) f = gcd(a - b, N) if (f != 1 and f != N): return f return -1
def problem5(): N = 2461799993978700679 print('generando primelist ...') primelist = primes(10000) print('buscando candidatos ...') roots, rowlist = find_candidates(N, primelist) print('calculando M ...') M = echelon.transformation_rows(rowlist) print('extraccion de v ...') v = M[-2] print('busqueda de a y b ...') a, b = find_a_and_b(v, roots, N) amb = a - b print('buscando gcd ...') print(gcd(amb, N))
def root_method(n): for a in range(intsqrt(n), n): b = intsqrt(a**2 - n) if a**2 - b**2 == n: return a - b return n ## Task 7.8.3 if PRINT_BOOK_TESTS: print("== 7.8.3 ==") n_3 = 367160330145890434494322103 a_3 = 67469780066325164 b_3 = 9429601150488992 print((a_3**2 - b_3**2) % n_3 == 0) print(gcd(n_3, a_3 - b_3)) ## Task 7.8.4 if PRINT_BOOK_TESTS: print("== 7.8.4 ==") prime_set = set([2, 3, 5, 7, 11, 13]) print(dumb_factor(12, prime_set)) print(dumb_factor(154, prime_set)) print(dumb_factor(2*3*3*3*11*11*13, prime_set)) print(dumb_factor(2*17, prime_set)) print(dumb_factor(2*3*5*7*19, prime_set)) ## Task 1 def int2GF2(i):
def gcd(x,y): return x if y == 0 else gcd(y, x % y) def problem5():
## Ungraded Task ## Find integers a and b such that a^2 - b^2 == N .. ## Writing an algorithim for this problem is too hard, ## either the program runs forever or it just returns ## a trivial divisor ... def root_method(N): a = intsqrt(N) + 1 while not isinstance(sqrt(a**2 - N), int): a += 1 b = sqrt( a**2 - N ) return a - b r, s, t = randint(1,10000000), randint(1,10000000), randint(1, 10000000) a = r * s b = s * t d = gcd(a, b) print(a % d == 0) print(b % d == 0) print(d >= s) ## Task 1 def int2GF2(i): ''' Returns one if i is odd, 0 otherwise. Input: - i: an int Output: - one if i is congruent to 1 mod 2 ( an odd number ) - 0 if i is congruent to 0 mod 2 ( an even number )
b = intsqrt(prod([root**2 - N for root in alist])) return (a, b) v = Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, { 0: 0, 1: one, 2: one, 4: 0, 5: one, 11: one }) N = 2419 roots = [51, 52, 53, 58, 61, 62, 63, 67, 68, 71, 77, 79] ##print(find_a_and_b(v, roots, N)) v = Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {0: 0, 1: 0, 10: one, 2: one}) N = 2419 roots = [51, 52, 53, 58, 61, 62, 63, 67, 68, 71, 77, 79] ##print(find_a_and_b(v, roots, N)) ## Task 5 N = 2461799993978700679 ##N=243127 primeset = primes(10000) candidates = find_candidates(N, primeset) transformation_matrix = echelon.transformation_rows(candidates[1]) (a, b) = find_a_and_b(transformation_matrix[-1], candidates[0], N) print(a, b) print(gcd(a - b, N)) smallest_nontrivial_divisor_of_2461799993978700679 = gcd(a - b, N)
Output: a pair (a,b) of integers such that a*a-b*b is a multiple of N (if v is correctly chosen) ''' a_factors = [roots[i] for i in v.f if v[i] == one] a = prod(a_factors) c_factors = [x * x - N for x in a_factors] c = prod(c_factors) b = intsqrt(c) assert b * b == c, "a*a - N is not a perfect square" return (a, b) ## Task 5 if __name__ == "__main__": primelist = primes(10000) N = 2461799993978700679 (roots, rowlist) = find_candidates(N, primelist) M = echelon.transformation_rows(rowlist, sorted(primelist, reverse=True)) divisor = N #starting at the largest possible divisor for i in range(2, 20): v = M[-i] (a, b) = find_a_and_b(v, roots, N) x = gcd(a - b, N) if x < divisor and x != 1: divisor = x if N // x < divisor and x != N: divisor = N // x print("The smallest found divisor is: {0}".format(divisor)) smallest_nontrivial_divisor_of_2461799993978700679 = 1230926561
b = sqrt(d) #print(str(i) + ':' + str(b)) if b%1 == 0: return int(i-b) return None N = 367160330145890434494322103 a = 67469780066325164 b = 9429601150488992 d = a*a - b*b #print(d) #print(d%N == 0) g = gcd(a-b, N) #print(g) #print (N%g == 0) r = 99083208324108213 s = 54982011313211212 t = 23239982309329329 a = r*s b = s*t d = gcd(a, b) #print(a%d == 0) #print(b%d == 0) #print(d > s) R,L = find_candidates(2419, primes(32))
b = intsqrt(c) assert b**2 == c return (a, b) ## Task 5 nontrivial_divisor_of_2461799993978700679 = None N = 2461799993978700679 primelist = primes(10000) start_long_time = time.time() roots, rowlist = find_candidates(N, primelist) M = echelon.transformation_rows(rowlist) for row in reversed(M): a, b = find_a_and_b(row, roots, N) divisor = gcd(a - b, N) if divisor != 1 and divisor != N: nontrivial_divisor_of_2461799993978700679 = divisor break elapsed_long_time = time.time() - start_long_time print("Found nontrivial divisor: ", nontrivial_divisor_of_2461799993978700679, " inefficiently in time: ", elapsed_long_time) nontrivial_divisor_of_2461799993978700679 = None start_short_time = time.time() roots, rowlist = find_candidates(N, primelist) M = echelon.transformation_rows(rowlist, sorted(primelist, reverse=True)) for row in reversed(M): a, b = find_a_and_b(row, roots, N) divisor = gcd(a - b, N)
def gcd(x,y): return x if y == 0 else gcd(y, x % y)
def gcd(x,y): return x if y == 0 else gcd(y, x % y) ## Task 5 smallest_nontrivial_divisor_of_2461799993978700679 = 1230926561
a = prod(a_factors) c_factors = [x*x - N for x in a_factors] c = prod(c_factors) b = intsqrt(c) assert b*b == c, "a*a - N is not a perfect square" return(a, b) ## Task 5 if __name__ == "__main__": primelist = primes(10000) N = 2461799993978700679 (roots, rowlist) = find_candidates(N, primelist) M = echelon.transformation_rows(rowlist, sorted(primelist, reverse=True)) divisor = N #starting at the largest possible divisor for i in range(2,20): v = M[-i] (a, b) = find_a_and_b(v, roots, N) x = gcd(a-b, N) if x < divisor and x != 1: divisor = x if N//x < divisor and x != N: divisor = N//x print("The smallest found divisor is: {0}".format(divisor)) smallest_nontrivial_divisor_of_2461799993978700679 = 1230926561
a pair (a,b) of integers such that a*a-b*b is a multiple of N (if v is correctly chosen) ''' alist = [roots[index] for index in range(len(roots)) if v[index] == one] ##print(alist) a = prod(alist) b = intsqrt(prod([root**2-N for root in alist])) return (a,b) v = Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: 0, 1: one, 2: one, 4: 0, 5: one, 11: one}) N = 2419 roots = [51, 52, 53, 58, 61, 62, 63, 67, 68, 71, 77, 79] ##print(find_a_and_b(v, roots, N)) v = Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: 0, 1: 0, 10: one, 2: one}) N = 2419 roots = [51, 52, 53, 58, 61, 62, 63, 67, 68, 71, 77, 79] ##print(find_a_and_b(v, roots, N)) ## Task 5 N = 2461799993978700679 ##N=243127 primeset = primes(10000) candidates = find_candidates(N, primeset) transformation_matrix = echelon.transformation_rows(candidates[1]) (a,b) = find_a_and_b(transformation_matrix[-1], candidates[0], N) print(a,b) print(gcd(a-b, N)) smallest_nontrivial_divisor_of_2461799993978700679 = gcd(a-b, N)
def gcd(x, y): return x if y == 0 else gcd(y, x % y)
factors = dumb_factor(x * x - N, primeset) if len(factors) > 0: roots.append(x) rowlist.append(make_Vec(primeset, factors)) if len(roots) >= len(primeset) + 1: break return (roots, rowlist) print(root_method(55)) print(root_method(77)) print(root_method(146771)) # print(root_method(118)) print(gcd(23, 15)) N = 367160330145890434494322103 a = 67469780066325164 b = 9429601150488992 c = a * a - b * b print(gcd(a - b, N)) print(make_Vec({2, 3, 5, 7, 11}, [(3, 1)])) print(make_Vec({2, 3, 5, 7, 11}, [(2, 17), (3, 0), (5, 1), (11, 3)])) print(find_candidates(2419, primes(32))[1]) A = listlist2mat([[0, 2, 3, 4, 5], [0, 0, 0, 3, 2], [1, 2, 3, 4, 5], [0, 0, 0, 6, 7], [0, 0, 0, 9, 8]]) print(A) M = transformation(A) print(M)
# rowIndex = -2 # roots, rowlist = find_candidates(N, primes(32)) # M = transformation_rows(rowlist) # print(roots, M[rowIndex]) # a,b = find_a_and_b(M[rowIndex], roots, N) # print(a, b, a-b, gcd(a-b, N), N/gcd(a-b, N)) ## Task 5 # N = 2461799993978700679 # rowIndex = -1 # roots, rowlist = find_candidates(N, primes(1000)) # M = transformation_rows(rowlist) # print(roots, M[rowIndex]) # a,b = find_a_and_b(M[rowIndex], roots, N) # print(a, b, a-b, gcd(a-b, N), N/gcd(a-b, N)) smallest_nontrivial_divisor_of_2461799993978700679 = 1230926561 print(datetime.now().time()) N = 20672783502493917028427 rowIndex = -1 roots, rowlist = find_candidates(N, primes(1000)) M = transformation_rows(rowlist) print(roots, M[rowIndex]) a, b = find_a_and_b(M[rowIndex], roots, N) print(a, b, a - b, gcd(a - b, N), N / gcd(a - b, N)) print(datetime.now().time())