首页 > 代码库 > 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的四次方是5177499508290240983296000015365639的四次方是5574456138713352372420977904118796760的四次方是12483374090995285495480576000020615673的四次方是180630077292169281088848499041true

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

以下测试运行用时

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开始循环有效的降低了运行时间大概达到原先的四分之一耗时。

Euler猜想