Project Euler:Problem 66 Diophantine equation

Consider quadratic Diophantine equations of the form:

x2 – Dy2 = 1

For example, when D=13, the minimal solution in x is 6492 – 13×1802 = 1.

It can be assumed that there are no solutions in positive integers when D is square.

By finding minimal solutions in x for D = {2, 3, 5, 6, 7}, we obtain the following:

32 – 2×22 = 1
22 – 3×12 = 1
92 – 5×42 = 1
52 – 6×22 = 1
82 – 7×32 = 1

Hence, by considering minimal solutions in x for D ≤ 7, the largest x is obtained when D=5.

Find the value of D ≤ 1000 in minimal solutions of x for which the largest value of x is obtained.


首先要了解佩尔方程及其解法

还要了解一些连分式的性质




import math

maxn=1
maxD=2
for D in range(2,1001):
    tmp=math.sqrt(D)
    tmp=int(tmp)
    if tmp*tmp==D:
        continue

    m=0
    d=1
    a=tmp
    #print(a)
    n1=1
    d1=0
    num=a
    den=1

    while num*num-D*den*den!=1:
        m=int(a*d-m)
        d=int((D-m*m)/d)
        a=int((tmp+m)/d)

        n2=n1
        n1=num
        d2=d1
        d1=den

        num=int(a*n1+n2)
        den=int(a*d1+d2)
        #print('num = ',num)
        #print('den = ',den)
        #print ('result = ',num*num-D*den*den)
    if num >maxn:
        maxn=num
        maxD=D
print(maxD)
     


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值