Chinaunix首页 | 论坛 | 博客
  • 博客访问: 86521
  • 博文数量: 47
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 625
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-11 12:11
文章分类

全部博文(47)

文章存档

2008年(47)

我的朋友

分类:

2008-11-24 19:03:19

Problem 66

26 March 2004

Consider quadratic Diophantine equations of the form:

x2 – Dy2 = 1

For example, when D=13, the minimal solution in x is 6492 – 131802 = 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 – 222 = 1
22 – 312 = 1
92 – 542 = 1
52 – 622 = 1
82 – 732 = 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.

x2 – Dy2 = 1这个方程叫做pell方程,对于非完全平方数D, X, Y有无数个解

求解方法 一般用的是连分数法

求出SQRT(D)的连分数[a0:(a1, a2, a3 ... an)...],其中an=2*a1

然后再计算

根据连分数循环节位数n的奇偶性分两种情况

若n为偶数,则 x= pn-1 , y= qn-1 ; n为奇数时x= p2n-1 , y= q2n-1

p0= a0, p1= a0* a1+1 , pn= an*pn-1+pn-2
q0=1,   q1=a1,        qn=an*qn-1+qn-2

注:以上说的解均为最小正解

def getSqrtFraction(num):
    result = []
    z     = int(num**0.5)
    result = [z]
    if z*z == num:
        return result

    d     = {}#用于保存中间已计算变量
    fm    = (num, z)
    fz    = 1
    while 1:
        k = [fm, fz]
        if d.has_key(str(k)):
            return result
        else:
            d[str(k)] = True
            t = (num - z**2)/fz
            result.append(int((num**0.5+z)/t))
            z = (int((num**0.5+z)/t))*t - z
            fm = (num, z)
            fz = t


def fun66():
    result, rnum = 0, 0
    for i in range(2, 1001):
        fc = getSqrtFraction(i)

        k  = len(fc)
        if== 0:
            continue
        if k == 1:
            continue
        p0 = fc[0]
        p1 = fc[0]*fc[1]+1
    
        n  = k - 1
        if n%2 == 0:#循环链为偶数
            t  = 2
            pn = p1
            while t < n:
                pn = fc[t]*p1 + p0
                p0, p1 = p1, pn
                t += 1
            if result < pn:
                result = pn
        else:#循环链长度为奇数
            t  = 2
            pn = p1
            while t < 2*n:
                if t <= n:
                    pn = fc[t]*p1 + p0
                else:
                    pn = fc[t%n]*p1 + p0
                p0, p1 = p1, pn
                t += 1
            if result < pn:
                rnum   = i
                result = pn
    return result, rnum

getSqrtFraction(num)用于获取num的连分数
answer is 661, x=16421658242965910275055840472270471049
time:0.125
阅读(696) | 评论(0) | 转发(0) |
0

上一篇:Problem 64

下一篇:Problem 67

给主人留下些什么吧!~~