-1

I am getting mad errors when i try to implement this program i wrote to solve Leonhard Euler's conjecture. The error seems to be in the println. Do you know what I'm doing wrong? (There are no errors before i run the program, the error messages appear after) What I'm implementing is fairly simple so I'm not quite sure why it is not cooperating.

p.s. I read on another website to assign the out message as a String object and print that String object, but that just adds another error message to the list.

    public static void main(String[] args) {

    BigInteger max = new BigInteger("Integer.MAX_VALUE");

    // for(int a=0; a<max; a++)
    for(BigInteger a=BigInteger.ZERO; a.compareTo(max)<=0; a=a.add(BigInteger.ONE)){

        for(BigInteger b=BigInteger.ZERO; b.compareTo(max)<=0; b=b.add(BigInteger.ONE)){

            for(BigInteger c=BigInteger.ZERO; c.compareTo(max)<=0; c=c.add(BigInteger.ONE)){

                for(BigInteger d=BigInteger.ZERO; d.compareTo(max)<=0; d=d.add(BigInteger.ONE)){

                    // a^4
                    a=a.pow(4);
                    // b^4
                    b=b.pow(4);
                    // c^4
                    c=c.pow(4);
                    // d^4
                    d=d.pow(4);

                    // a+b+c
                    BigInteger sum = new BigInteger("a.add(b).add(c)");

                    // if( sum == d^4 )
                    int euler = sum.compareTo(d);
                    if( euler ==0)
                    {
                        System.out.println(a+"^4+"+b+"^4+"+c+"^4="+d+"^4");
                    }  
                }
            }
        }
    }   
}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 6
    BigInteger max = new BigInteger("Integer.MAX_VALUE"); <-- I don't think you want the quotes here.... – Amir Afghani Apr 09 '15 at 18:32
  • 2
    Same here: `BigInteger sum = new BigInteger("a.add(b).add(c)");`. Which, of course, could simply be `BigInteger sum = a.add(b).add(c);` – GriffeyDog Apr 09 '15 at 18:33

3 Answers3

1

I guess the problem is more with the construction of the sum:

BigInteger sum = new BigInteger("a.add(b).add(c)");

Java in general is not an interpreted language: it loses track of the name of the variables.

Also if you do:

d=d.pow(4);

d will keep it's value for ever (not take the old one after the loop).

You also use:

new BigInteger("Integer.MAX_VALUE");

Which again cannot be resolved, try:

BigInteger.valueOf(Integer.MAX_VALUE);

The errors are probably resolved using:

BigInteger max = BigInteger.valueOf(Integer.MAX_VALUE);

// for(int a=0; a<max; a++)
for(BigInteger a=BigInteger.ZERO; a.compareTo(max)<=0; a=a.add(BigInteger.ONE)){

    for(BigInteger b=BigInteger.ZERO; b.compareTo(max)<=0; b=b.add(BigInteger.ONE)){

        for(BigInteger c=BigInteger.ZERO; c.compareTo(max)<=0; c=c.add(BigInteger.ONE)){

            for(BigInteger d=BigInteger.ZERO; d.compareTo(max)<=0; d=d.add(BigInteger.ONE)){

                // a^4
                BigInteger a4=a.pow(4);
                // b^4
                BigInteger b4=b.pow(4);
                // c^4
                BigInteger c4=c.pow(4);
                // d^4
                BigInteger d4=d.pow(4);

                // a+b+c
                BigInteger sum = a4.add(b4).add(c4);

                // if( sum == d^4 )
                int euler = sum.compareTo(d4);
                if( euler ==0)
                {
                    System.out.println(a+"^4+"+b+"^4+"+c+"^4="+d+"^4");
                }  
            }
        }
    }
}

If we limit max to 4, it generates:

0^4+0^4+0^4=0^4
0^4+0^4+1^4=1^4
0^4+0^4+2^4=2^4
0^4+0^4+3^4=3^4
0^4+0^4+4^4=4^4
0^4+1^4+0^4=1^4
0^4+2^4+0^4=2^4
0^4+3^4+0^4=3^4
0^4+4^4+0^4=4^4
1^4+0^4+0^4=1^4
2^4+0^4+0^4=2^4
3^4+0^4+0^4=3^4
4^4+0^4+0^4=4^4

Furthermore the algorithm you design is not very efficient. A better way to do this is by setting an upperbound (max), iterate with the variables up to that upperbound, and then increment the bound. Otherwise, it will take approximately 2^62 before a is set to 1 at all.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
1

Two things I can spot immediately as 'probably wrong'.

BigInteger max = new BigInteger("Integer.MAX_VALUE");

What you probably want is:

BigInteger max = BigInteger.valueOf((long)Integer.MAX_VALUE);

And also:

BigInteger sum = new BigInteger("a.add(b).add(c)");

Try instead:

BigInteger sum = a.add(b).add(c)
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
1

@CommuSoft identified your errors (so accept his answer), but I wanted to point out that you're going a bit crazy with the BigIntegers, and that you're performing a lot of redundant computations. Insanely many. This would be a lot more efficient:

public static void main(String[] args) {
    for (int a = -1; a++ < Integer.MAX_VALUE; ) {
        BigInteger a4 = BigInteger.valueOf(a).pow(4);

        for (int b = a - 1; b++ < Integer.MAX_VALUE; ) {
            BigInteger b4 = BigInteger.valueOf(b).pow(4);
            BigInteger partialSum = a4.add(b4);

            for(int c = b - 1; c++ < Integer.MAX_VALUE; ) {
                BigInteger c4 = BigInteger.valueOf(c).pow(4);
                BigInteger sum = partialSum.add(c4);

                for(int d = c - 1; d++ < Integer.MAX_VALUE; ) {
                    BigInteger d4 = BigInteger.valueOf(d).pow(4);

                    int euler = sum.compareTo(d4);
                    if( euler == 0)
                    {
                        System.out.println(a4+"^4+"+b4+"^4+"+c4+"^4="+d4+"^4");
                    } else if (euler < 0) {
                        // d4 is larger than the sum, and will only get bigger
                        break;
                    }
                }
            }
        }
    }   
}

Even with those changes, though, I don't see this code running to completion in your lifetime. In fact, it probably does not run to completion in the lifetime of the universe.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • 1
    Thank you so much! I am obviously a noob and wanted to experiment with the method of exhaustion. Thank you for your knowledge, insight, and improvements! – shpadoinklejoe Apr 09 '15 at 19:20