def diophantine_factorization_pell_equation(D, N): if D == 1 and N >= 1: print "==============================================================================" print "Pell Diophantine Equation - (exponential time) Factorization by solving Pell's Equation (for D=1 and some N)" print "==============================================================================" sol = diop_DN(D, N) soln = sol[0] print "Solution for Pell Equation : x^2 - ", D, "*y^2 = ", N, ":" print "(", soln[0], " + ", soln[1], ") * (", soln[0], " - ", soln[ 1], ") = ", N if D == 1 and N == -1: #Invocation of factorization is commented because of Spark Accumulator broadcast issue which #probably requires single Spark Context across complementation and factoring. Presently #factors are persisted to a json file and read offline by complementation #DiscreteHyperbolicFactorizationUpperbound_TileSearch_Optimized.SearchTiles_and_Factorize(N) factorsfile = open( "DiscreteHyperbolicFactorizationUpperbound_TileSearch_Optimized.factors" ) factors = json.load(factorsfile) number_to_factorize = 0 for k, v in factors.iteritems(): number_to_factorize = k factorslist = v print "==============================================================================" print "Pell Diophantine Equation - (polynomial time) PRAM-NC Factorization solving Pell's Equation (for D=1 and N=", number_to_factorize, ")" print "==============================================================================" for f in factorslist: p = f q = int(number_to_factorize) / p x = (p + q) / 2 y = (p - q) / 2 print "Solution for Pell Equation : x^2 - ", D, "*y^2 = ", number_to_factorize, ":" print "x=", x, "; y=", y
def diophantine_factorization_pell_equation(D,N): if D==1 and N >= 1: print "==============================================================================" print "Pell Diophantine Equation - (exponential time) Factorization by solving Pell's Equation (for D=1 and some N)" print "==============================================================================" sol=diop_DN(D,N) soln=sol[0] print "Solution for Pell Equation : x^2 - ",D,"*y^2 = ",N,":" print "(",soln[0]," + ",soln[1],") * (",soln[0]," - ",soln[1],") = ",N if D==1 and N==-1: #Invocation of factorization is commented because of Spark Accumulator broadcast issue which #probably requires single Spark Context across complementation and factoring. Presently #factors are persisted to a json file and read offline by complementation #DiscreteHyperbolicFactorizationUpperbound_TileSearch_Optimized.SearchTiles_and_Factorize(N) factorsfile=open("DiscreteHyperbolicFactorizationUpperbound_TileSearch_Optimized.factors") factors=json.load(factorsfile) number_to_factorize=0 for k,v in factors.iteritems(): number_to_factorize=k factorslist=v print "==============================================================================" print "Pell Diophantine Equation - (polynomial time) PRAM-NC Factorization solving Pell's Equation (for D=1 and N=",number_to_factorize,")" print "==============================================================================" for f in factorslist: p=f q=int(number_to_factorize)/p x=(p+q)/2 y=(p-q)/2 print "Solution for Pell Equation : x^2 - ",D,"*y^2 = ",number_to_factorize,":" print "x=",x,"; y=",y
def main(): max_x = 0 max_D = 0 for D in range(1, 1001): solution = diop_DN(D, 1)[0] if solution[0] > max_x: max_x = solution[0] max_D = D return max_D
def euler066(top): maxX = 0 for D in range(top + 1): if not ef.is_square(D): solution = diop_DN(D, 1) x = solution[0][0] # y= solution[0][1] # print("{}^2 - {}x {}^2 = 1".format(x, D, y)) if x > maxX: maxX = x maxTuple = x, D return maxTuple[1]
def minimal_x_DN(D): return min(x for (x, y) in diop_DN(D, 1))
def test_DN(): # Most of the test cases were adapted from, # Solving the generalized Pell equation x**2 - D*y**2 = N, John P. Robertson, July 31, 2004. # http://www.jpr2718.org/pell.pdf # others are verified using Wolfram Alpha. # Covers cases where D <= 0 or D > 0 and D is a square or N = 0 # Solutions are straightforward in these cases. assert diop_DN(3, 0) == [(0, 0)] assert diop_DN(-17, -5) == [] assert diop_DN(-19, 23) == [(2, 1)] assert diop_DN(-13, 17) == [(2, 1)] assert diop_DN(-15, 13) == [] assert diop_DN(0, 5) == [] assert diop_DN(0, 9) == [(3, t)] assert diop_DN(9, 0) == [(3 * t, t)] assert diop_DN(16, 24) == [] assert diop_DN(9, 180) == [(18, 4)] assert diop_DN(9, -180) == [(12, 6)] assert diop_DN(7, 0) == [(0, 0)] # When equation is x**2 + y**2 = N # Solutions are interchangeable assert diop_DN(-1, 5) == [(2, 1), (1, 2)] assert diop_DN(-1, 169) == [(12, 5), (5, 12), (13, 0), (0, 13)] # D > 0 and D is not a square # N = 1 assert diop_DN(13, 1) == [(649, 180)] assert diop_DN(980, 1) == [(51841, 1656)] assert diop_DN(981, 1) == [(158070671986249, 5046808151700)] assert diop_DN(986, 1) == [(49299, 1570)] assert diop_DN(991, 1) == [(379516400906811930638014896080, 12055735790331359447442538767)] assert diop_DN(17, 1) == [(33, 8)] assert diop_DN(19, 1) == [(170, 39)] # N = -1 assert diop_DN(13, -1) == [(18, 5)] assert diop_DN(991, -1) == [] assert diop_DN(41, -1) == [(32, 5)] assert diop_DN(290, -1) == [(17, 1)] assert diop_DN(21257, -1) == [(13913102721304, 95427381109)] assert diop_DN(32, -1) == [] # |N| > 1 # Some tests were created using calculator at # http://www.numbertheory.org/php/patz.html assert diop_DN(13, -4) == [(3, 1), (393, 109), (36, 10)] # Source I referred returned (3, 1), (393, 109) and (-3, 1) as fundamental solutions # So (-3, 1) and (393, 109) should be in the same equivalent class assert equivalent(-3, 1, 393, 109, 13, -4) == True assert diop_DN(13, 27) == [(220, 61), (40, 11), (768, 213), (12, 3)] assert set(diop_DN(157, 12)) == \ set([(13, 1), (10663, 851), (579160, 46222), \ (483790960,38610722), (26277068347, 2097138361), (21950079635497, 1751807067011)]) assert diop_DN(13, 25) == [(3245, 900)] assert diop_DN(192, 18) == [] assert diop_DN(23, 13) == [(-6, 1), (6, 1)] assert diop_DN(167, 2) == [(13, 1)] assert diop_DN(167, -2) == [] assert diop_DN(123, -2) == [(11, 1)] # One calculator returned [(11, 1), (-11, 1)] but both of these are in # the same equivalence class assert equivalent(11, 1, -11, 1, 123, -2) assert diop_DN(123, -23) == [(-10, 1), (10, 1)] assert diop_DN(0, 0, t) == [(0, t)] assert diop_DN(0, -1, t) == []
def test_DN(): # Most of the test cases were adapted from, # Solving the generalized Pell equation x**2 - D*y**2 = N, John P. Robertson, July 31, 2004. # http://www.jpr2718.org/pell.pdf # others are verified using Wolfram Alpha. # Covers cases where D <= 0 or D > 0 and D is a square or N = 0 # Solutions are straightforward in these cases. assert diop_DN(3, 0) == [(0, 0)] assert diop_DN(-17, -5) == [] assert diop_DN(-19, 23) == [(2, 1)] assert diop_DN(-13, 17) == [(2, 1)] assert diop_DN(-15, 13) == [] assert diop_DN(0, 5) == [] assert diop_DN(0, 9) == [(3, t)] assert diop_DN(9, 0) == [(3*t, t)] assert diop_DN(16, 24) == [] assert diop_DN(9, 180) == [(18, 4)] assert diop_DN(9, -180) == [(12, 6)] assert diop_DN(7, 0) == [(0, 0)] # When equation is x**2 + y**2 = N # Solutions are interchangeable assert diop_DN(-1, 5) == [(2, 1)] assert diop_DN(-1, 169) == [(12, 5), (0, 13)] # D > 0 and D is not a square # N = 1 assert diop_DN(13, 1) == [(649, 180)] assert diop_DN(980, 1) == [(51841, 1656)] assert diop_DN(981, 1) == [(158070671986249, 5046808151700)] assert diop_DN(986, 1) == [(49299, 1570)] assert diop_DN(991, 1) == [(379516400906811930638014896080, 12055735790331359447442538767)] assert diop_DN(17, 1) == [(33, 8)] assert diop_DN(19, 1) == [(170, 39)] # N = -1 assert diop_DN(13, -1) == [(18, 5)] assert diop_DN(991, -1) == [] assert diop_DN(41, -1) == [(32, 5)] assert diop_DN(290, -1) == [(17, 1)] assert diop_DN(21257, -1) == [(13913102721304, 95427381109)] assert diop_DN(32, -1) == [] # |N| > 1 # Some tests were created using calculator at # http://www.numbertheory.org/php/patz.html assert diop_DN(13, -4) == [(3, 1), (393, 109), (36, 10)] # Source I referred returned (3, 1), (393, 109) and (-3, 1) as fundamental solutions # So (-3, 1) and (393, 109) should be in the same equivalent class assert equivalent(-3, 1, 393, 109, 13, -4) == True assert diop_DN(13, 27) == [(220, 61), (40, 11), (768, 213), (12, 3)] assert set(diop_DN(157, 12)) == \ set([(Integer(13), Integer(1)), (Integer(10663), Integer(851)), (Integer(579160), Integer(46222)), \ (Integer(483790960),Integer(38610722)), (Integer(26277068347), Integer(2097138361)), (Integer(21950079635497), Integer(1751807067011))]) assert diop_DN(13, 25) == [(3245, 900)] assert diop_DN(192, 18) == [] assert diop_DN(23, 13) == [(-6, 1), (6, 1)] assert diop_DN(167, 2) == [(13, 1)] assert diop_DN(167, -2) == [] assert diop_DN(123, -2) == [(11, 1)] # One calculator returned [(11, 1), (-11, 1)] but both of these are in # the same equivalence class assert equivalent(11, 1, -11, 1, 123, -2) assert diop_DN(123, -23) == [(-10, 1), (10, 1)]
from sympy.solvers.diophantine import diop_DN squares = [i**2 for i in range(int(100**(1 / 2)) + 1)] min_x = [] for i in range(2, 1001): if i in squares: continue min_x.append([i, *diop_DN(i, 1)]) print(max(min_x, key=lambda x: x[1]))
from sympy.solvers.diophantine import diop_DN nmax = 1000000; sum = 0; for i in range(1, 500): print(i, sum) D = 4*i*i+1 N = i*i sol = diop_DN(D, 1) print(i, sol) r = sol[0][0] s = sol[0][1] solFund = diop_DN(D, N) for sf in solFund: p=sf[0] q=sf[1] while(p<nmax): sum +=p; (p, q) = (p*r+D*s*q, p*s+r*q) #p is area (q i) print("sum=", sum)
''' A solution to the project euler problem #66 https://projecteuler.net/problem=66 ''' from sympy.solvers.diophantine import diop_DN print(sorted([(diop_DN(d, 1)[0], d) for d in range(2, 1001)])[-1][1])