1

I am trying to repeat the search for a counterexample for Euler's conjecture. I need to find five numbers such that a1^5+a2^5+3^5+a4^5 =a5^5. The smallest counterexample is 27^5+84^5+110^5+133^5=144^5. I think I am looping over each number and make sure that the numbers are ordered, however, I don't get the expected result. Please point my error.

public class Euler {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int a1 = 1; //greatest
        int a2 = 1;
        int a3 = 1;
        int a4 = 1;  
        boolean solved = false;
        while (!solved) {
            if (a3 >= a4) { //major progress!!!
                System.out.println("values are " + a1 + " " + a2 + " " + a3 + " " + a4 + " " + solved);
                a4++;
                a3 = 1;
                a2 = 1;
                a1 = 1;
            }else if(a2 >= a3) {
                a3++;
                a2 = 1;
                a1 = 1;
            } else if(a1 >= a2) {
                a2++;
                a1 = 1;
            } 
            solved = antiEuler(a1, a2, a3, a4);//Check conjencture
            a1++;
        }    
    }    
    public static boolean antiEuler(int a1, int a2, int a3, int a4) {//

        double a5 = Math.pow(Math.sqrt(Math.pow(a1, 5) + Math.pow(a2, 5) + Math.pow(a3, 5) + Math.pow(a4, 5)), 0.2);
        double a6 = Math.floor(a5);
        if (a6 == a5) {
                        System.out.println("Done!");
            System.out.println(a1+" "+a2+" "+a3 +" "+a4+" "+a5);    


            return true;
        } else {
            return false;
        }
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
sixtytrees
  • 1,156
  • 1
  • 10
  • 25

2 Answers2

1

You're trying to do a direct comparison between two floating-point values, a5 and a6. If there are any precision issues in any of the calculations involved for either value, they may be off by a seemingly insignificant value, but will thus no longer be equal. You should rewrite your code to deal only in integers, calculating a5^5 instead of (a1^5+a2^5+3^5+a4^5)^(1/5). If this leads to overflow issues, then you should replace int with long or BigInteger.

Derek Peirce
  • 337
  • 5
  • 10
1

There are several issues with this program.

  • first, the smallest example contains 1445 which is greater than 6*1010, so int will not work. You should use long
  • second, you should compare integers, not their roots to avoid running into a near miss. This program finds the first example.

    public class Euler {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        long a1 = 1; //smallest 
        long a2 = 1;
        long a3 = 1;
        long a4 = 1;  //greatest
        boolean isSolved = false;
        double a6 = 0;
        double a7 = 0;
        long a8 = 0;
        double a9 = 0;
        while (!isSolved) {
            a1++;
            if (a1 > a2) {
                a2++;
                a1 = 1;
            }
            if (a2 > a3) {
                a3++;
                a2 = 1;
                a1 = 1;
            }
            if (a3 > a4) { 
                //System.out.println(a4 + " " + isSolved);
                a4++;
                a3 = 1;
                a2 = 1;
                a1 = 1;
            }
            a6 = Math.pow(a1, 5) + Math.pow(a2, 5) + Math.pow(a3, 5) + Math.pow(a4, 5);
            a7 = Math.pow(a6, 0.2);
    
            a8 = (long) a7;
            a9 = Math.pow(a8, 5);
    
            if (a6 == a9) {
                isSolved = true;
                System.out.println(a1 + " " + a2 + " " + a3 + " " + a4 + " " + a8);
            }           
        }
    }
    }
    
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
John
  • 45
  • 2