题目链接

脑补知识:佩尔方差

上面说的貌似很明白,最小的i,对应最小的解

然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题。i从0开始变量,知道第一个满足等式的解就是最小解。

问题转化为求根号n的连分数问题,分子就是x,分母就是y

要求的分子,分母,问题又转化为:根号n的连分数表示,对,求出其连分数表示就OK了

先求出a的序列是什么?

第64题,就是求a的序列的。

a求出来了,要求出分子分母的表达式。

第65题,就是已经知道了a的序列,求分子,当然也可以求分母的

分子,分母求出来了,在验证:X*X-D*Y*Y=1时候就是最小解

问题真是一环套一环的。

Python程序:

import time as time 

start = time.time()

def getD(N):
x_max ,y_max= 0,0
D = 0
x,y = 0,0
for S in range(2,N+1):
x,y = resolve(S)
if x>x_max:
x_max ,y_max= x,y
D = S
return D,x_max,y_max def resolve(S):
m = 0
d = 1
a0 = int(S**0.5)
if a0*a0 == S :return -1,-1;
a= a0
li = [a]
x,y = 1,1
while x*x-S*y*y!=1:
m = d*a - m
d = (S - m*m)/d
a = int((a0 + m)/d)
li.append(a)
x = getX(li)
y = getY(li)
# print li
return x,y; def getX(li):
x0 = 1
x1 = li[0]
li = li[1:]
for l in li:
x = l * x1 + x0
x0 = x1
x1 = x
return x def getY(li):
y0 = 0
y1 = 1
li = li[1:]
for l in li:
y = l * y1 + y0
y0 = y1
y1 = y
return y if __name__ == '__main__': start = time.time()
N = 1000
D ,x_max,y_max= getD(N)
print "running time={0}seconds,D={1},x_max={2},y_max={3}".format(time.time()-start,D,x_max,y_max)

求的是最小解X的最大值时候的D,答案是661

然而:

x_max=16421658242965910275055840472270471049

y_max=638728478116949861246791167518480580

这个值好大的

附一:python程序:

from math import sqrt
from time import time def prefect_sqrt(n):
return int(sqrt(n))**2 == n def floor_root(n):
return int(sqrt(n)) def chakravals(n):
x_max = 0
for d in range(2,n+1):
if not prefect_sqrt(d):
p1 = floor_root(d)
q1 = 1
m1 = p1**2 - d
# print -p1,(-p1) % abs(m1),(-p1) % abs(m1)
if (-p1) % abs(m1) ==0:
x1 = abs(m1)
else:
x1 = (-p1) % abs(m1) while m1!=1:
p0 = p1
q0 = q1
m0 = m1
x0 = x1 p1 = (p0 * x0 +d *q0)/abs(m0)
q1 = (p0 + x0)/abs(m0) m1 = (x0**2 -d)/m0 if (-x0)%abs(m1) ==0:
x1 = abs(x0)
else:
x1 = (-x0)%abs(m1)
if p1>x_max:
x_max = p1
d_max = d
print "d= %04d x = %d"%(d_max,x_max)
print if __name__=='__main__':
start = time()
chakravals(1000)
end = time()
print "time elapse=%f"%(end - start)

Java程序:

这个跑的好慢的

package project61;
import java.math.*; public class P66
{
public static final int precision = 500;
public static void main( String args[] )
{
BigInteger Max = new BigInteger("0");
int ans = 0; outer: for (int D_i = 2; D_i <= 1000; D_i++)
{
BigDecimal D = new BigDecimal(D_i);
BigDecimal SD = calculation.Sqrt(D); BigDecimal SD_i = SD.setScale(0, BigDecimal.ROUND_FLOOR);
if (SD_i.multiply(SD_i).equals(D))
continue; int a[] = calculation.toContinuedFraction(SD, 100); for (int i = 1; i < 100; i++)
{
Fraction temp = new Fraction(a[i],1); for (int j = i - 1; j >= 0; j--)
temp = Fraction.Compute(a[j], temp); BigInteger y_2 = temp.denominator.multiply(temp.denominator);
BigInteger x_2 = temp.numerator.multiply(temp.numerator);
BigInteger result = x_2.subtract(y_2.multiply(D.toBigIntegerExact())).subtract(BigInteger.ONE); if (result.equals(BigInteger.ZERO))
{
if (temp.numerator.compareTo(Max) > 0)
{
Max = temp.numerator;
ans = D_i;
} continue outer;
} }
System.out.print("Warning!\n"); }
System.out.print(ans+"\n");
}
} class Fraction
{
public BigInteger numerator;
public BigInteger denominator; private Fraction()
{ }
public Fraction (int numerator, int denominator)
{
this.numerator = BigInteger.valueOf(numerator);
this.denominator = BigInteger.valueOf(denominator);
}
public static Fraction Compute(int p1, Fraction p2)
{
Fraction ans = new Fraction();
ans.numerator = p2.denominator.add(p2.numerator.multiply(BigInteger.valueOf(p1)));
ans.denominator = p2.numerator.add(BigInteger.ZERO);
return ans;
}
} class calculation
{
private static final BigDecimal N0 = new BigDecimal(0);
private static final BigDecimal N1 = new BigDecimal(1);
private static final BigDecimal N2 = new BigDecimal(2); public static BigDecimal Sqrt(BigDecimal In)
{
BigDecimal N = new BigDecimal(1);
while(true)
{
BigDecimal NN = N.multiply(N);
NN = NN.add(In);
NN = NN.divide(N2);
NN = NN.divide(N, P66.precision, BigDecimal.ROUND_FLOOR); if (NN.equals(N))
break; N = NN;
} return N;
}
public static int[] toContinuedFraction(BigDecimal In, int l)
{
int ans[] = new int[l]; BigDecimal temp = In.add(N0);
for (int i = 0; i < l; i++)
{
ans[i] = Integer.valueOf(temp.setScale(0, BigDecimal.ROUND_FLOOR).toString()).intValue();
temp = temp.subtract(temp.setScale(0, BigDecimal.ROUND_FLOOR));
temp = N1.divide(temp, P66.precision, BigDecimal.ROUND_FLOOR);
}
return ans;
}
}

最后两个程序在网上复制过来的

欧拉工程第66题:Diophantine equation的更多相关文章

  1. 欧拉工程第69题:Totient maximum

    题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...

  2. 欧拉工程第70题:Totient permutation

    题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...

  3. 欧拉工程第67题:Maximum path sum II

    By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ma ...

  4. 欧拉工程第65题:Convergents of e

    题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...

  5. 欧拉工程第56题:Powerful digit sum

    题目链接   Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...

  6. 欧拉工程第55题:Lychrel numbers

    package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...

  7. 欧拉工程第54题:Poker hands

    package projecteuler51to60; import java.awt.peer.SystemTrayPeer; import java.io.BufferedReader; impo ...

  8. 欧拉工程第53题:Combinatoric selections

    package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...

  9. 欧拉工程第52题:Permuted multiples

    题目链接 题目: 125874和它的二倍,251748, 包含着同样的数字,只是顺序不同. 找出最小的正整数x,使得 2x, 3x, 4x, 5x, 和6x都包含同样的数字. 这个题目相对比较简单 暴 ...

随机推荐

  1. MSSQL优化之——查看语句执行情况

    MSSQL优化之——查看语句执行情况 在写SQL语句时,必须知道语句的执行情况才能对此作出优化.了解SQL语句的执行情况是每个写程序的人必不可少缺的能力.下面是对查询语句执行情况的方法介绍. 一.设置 ...

  2. js拖拽3D立方体旋转

    这段时间有点闲,所以就自己找些小玩意来练习练习.今天做了一个可以拖拽页面内空白位置3D立方体就会跟着转动的小例子,布局方面用到css3 3D转换技术,通过transform控制旋转实现的. 上个图 代 ...

  3. DirectoryEntry配置IIS出现ADSI Error:未知错误(0x80005000)

    目录 问题案例 原因分析 解决问题 总结 问题案例 DirectoryEntry配置IIS,在IIS6.0下运转正常,但IIS7.0下运转会出错: System.DirectoryServices.D ...

  4. SQL中的自定义函数Function

    先给出一个链接吧,别人写的:http://www.cnblogs.com/diony/archive/2010/12/17/1909014.html 总结得很全面,感谢感谢!自己练习了一下后面的例子, ...

  5. Game Tutorials

    SDL: http://www.sdltutorials.com/tutorials        http://lazyfoo.net/ http://panda3d.noie.name/ http ...

  6. objdump的使用方法和 symbol table的每列的含义

    一.objdump的用法 objdump命令的man手册 objdump     [-a] [-b bfname|     --target=bfdname] [-C] [--debugging]   ...

  7. hadoop自动安装的脚本与步骤

    最近要在10几台机器上安装hadoop.对于这种繁复而重复的工作,一步步的打命令行,对于程序员来说是一件不能忍的事情.所以我就琢磨着怎么写一个脚本来自动安装hadoop. 任务: 在10几台机器上中的 ...

  8. Python操作列表的常用方法

     下面列出列表常用的方法操作列表以及小例子:   1.  Append      在列表末尾添加元素,需在列表末尾添加元素,需要注意几个点:      A. append中添加的参数是作为一个整体 & ...

  9. iOS内存管理retain,assign,copy,strong,weak

    转自:http://www.cnblogs.com/nonato/archive/2013/11/28/3447162.html iOS的对象都继承于NSObject, 该对象有一个方法:retain ...

  10. nginx学习之一

    http://tengine.taobao.org/book/chapter_02.html