Euler猜想

Euler猜想

这是从http://duodaa.com/blog/index.php/archives/538/截得图,以下是代码

package math;

import java.math.BigDecimal;
import java.util.function.BiConsumer; public class TestEuler {
public static void main(String[] args) {
boolean flg=true; for(long x=1;flg;x++){
for(long y=1;flg&&(y<x);y++){
for(long z=1;flg&&(z<y);z++){
for(long w=1;true;w++){
int r=power4Long(w).compareTo(sum(power4Long(x),power4Long(y),power4Long(z)));
System.out.print(x+":"+power4Long(x).toString()+",");
System.out.print(y+":"+power4Long(y).toString()+",");
System.out.print(z+":"+power4Long(z).toString()+",");
System.out.println(w+":"+power4Long(w).toString()+";");
if(r==1){
break;
}
if(r==0){
flg=false;
break;
}
}
}
}
}
}
public static boolean checkEuler(long x,long y,long z,long w){
return power4Long(w).compareTo(sum(power4Long(x),power4Long(y),power4Long(z)))==0;
}
public static BigDecimal power4Long(Long b){
return power4(new BigDecimal(b));
} public static BigDecimal power4(BigDecimal b){
return b.multiply(b).multiply(b).multiply(b);
}
public static BigDecimal sum(BigDecimal... bs){
BigDecimal reB=new BigDecimal(0);
for(BigDecimal b:bs){
reB=reB.add(b);
}
return reB;
}
}

事实上这样的四层循环极大的消耗着计算机的性能计算很慢,要考我的这些代码来验证欧拉猜想估计得跑到我死都没结果

所以一下代码直接验证下结果

package math;

public class TestEuler2 {
public static void main(String[] args) {
long x=2682440L;
long y=15365639L;
long z=18796760L;
long w=20615673L;
System.err.println(x+"的四次方是"+TestEuler.power4Long(x).toString());
System.err.println(y+"的四次方是"+TestEuler.power4Long(y).toString());
System.err.println(z+"的四次方是"+TestEuler.power4Long(z).toString());
System.err.println(w+"的四次方是"+TestEuler.power4Long(w).toString());
System.out.println(TestEuler.checkEuler(x, y, z, w));
}
}

  此代码结果如下

2682440的四次方是51774995082902409832960000
15365639的四次方是55744561387133523724209779041
18796760的四次方是124833740909952854954805760000
20615673的四次方是180630077292169281088848499041
true

  有人证明这个方程式有无穷的解,真是让人惊叹数学的深邃伟大。

以下测试运行用时

package math;

import java.math.BigDecimal;
import java.util.function.BiConsumer; import org.jgroups.tests.perf.Data; /**
* @author zxl
* @jdk 1.8
* @Date 2016年10月13日上午10:04:24
*/
public class TestEuler {
public static void main(String[] args) {
long currTime=System.currentTimeMillis(); boolean flg=true; for(long x=1;flg&&(x<10L);x++){
for(long y=1;flg&&(y<x);y++){
for(long z=1;flg&&(z<y);z++){
for(long w=1;true;w++){
int r=power4Long(w).compareTo(sum(power4Long(x),power4Long(y),power4Long(z)));
System.out.print(x+":"+power4Long(x).toString()+",");
System.out.print(y+":"+power4Long(y).toString()+",");
System.out.print(z+":"+power4Long(z).toString()+",");
System.out.println(w+":"+power4Long(w).toString()+";");
if(r==1){
break;
}
if(r==0){
flg=false;
break;
}
}
}
}
}
System.out.println("用时共计:"+(System.currentTimeMillis()-currTime));
}
public static boolean checkEuler(long x,long y,long z,long w){
return power4Long(w).compareTo(sum(power4Long(x),power4Long(y),power4Long(z)))==0;
}
public static BigDecimal power4Long(Long b){
return power4(new BigDecimal(b));
} public static BigDecimal power4(BigDecimal b){
return b.multiply(b).multiply(b).multiply(b);
}
public static BigDecimal sum(BigDecimal... bs){
BigDecimal reB=new BigDecimal(0);
for(BigDecimal b:bs){
reB=reB.add(b);
}
return reB;
}
}

 该代码计算到10用时163毫秒,因为w在小于x的时候等式恒不成立

for(long w=x;true;w++)

所以w从x开始循环有效的降低了运行时间大概达到原先的四分之一耗时。

上一篇:轻松实现Android,iOS的一个手势动画效果


下一篇:关于 m4 文本处理引擎