Hi,
I'm trying to find the number abcd such that a^b + c^d = abcd
however, I'm running the code fine, but it's going to my second if statement, the one that's supposed to tell me that there's no solution. Am I doing something wrong or is there truly no solution?

public class numbertwo {

    public static void main(String[] args) {
    	double first = 0;
    	double second = 0;
    	double third = 0;
    	double fourth = 0;
    	double d = 1;

    	while (d != 0 && first < 9) {
    		first++;
    		while (d != 0 && second < 9) {
    			second++;
    			while (d != 0 && third < 9) {
    				third++;
    				while (d != 0 && fourth < 9) {
    					fourth++;
    					d = Math.pow(first,second) + Math.pow(third,fourth) - first * 1000 - second * 100 - third * 10 - fourth;
    				}
    				d = Math.pow(first,second) + Math.pow(third,fourth) - first * 1000 - second * 100 - third * 10 - fourth;
    			}
    			d = Math.pow(first,second) + Math.pow(third,fourth) - first * 1000 - second * 100 - third * 10 - fourth;
    		}
    		d = Math.pow(first,second) + Math.pow(third,fourth) - first * 1000 - second * 100 - third * 10 - fourth;
    	}
    	if ( d == 0 ) {
    		System.out.print("Win");
    		System.out.println(first * 1000 + second * 100 + third * 10 + fourth);
    	}
    	if ( d != 0 ) {
    		System.out.print("Bleh noob");
    		d = 0;
    	}

    }


}

first things first, you're using floating point numbers where you should be using integer numbers.
Not only is that uncalled for here, but due to floating point rounding errors you'll never get (unless by pure chance) the result of a floating point calculation to be exactly 0.

Solve that first and then try again. I've not bothered checking the rest of your code which tbh looks a bit too convoluted to be correct (but there might not be an easier way, I've not looked into that), as there's no point to if whatever it results in will never be what you expect because of your incorrect choice of datatypes.

I don't understand. If I use integer numbers wouldn't I risk getting a rounding error? I want d precisely equal to zero so that I know that a^b + c^d is exactly equal to abcd.

And what makes you think that equation would ever yield a floating point number given that all the operands are integers by definition?

a,b,c, and d MUST be integer numbers else your "abcd" is not a number...

Edited 6 Years Ago by jwenting: n/a

public class numbertwo {

    public static void main(String[] args) {
    	int first = 0;
    	int second = 0;
    	int third = 0;
    	int fourth = 0;
    	double d = 1;

    	while (d != 0 || first < 9) {
    		second = 0;
    		third = 0;
    		fourth = 0;
    		first++;
    		d = Math.pow(first,second) + Math.pow(third,fourth) - first * 1000 - second * 100 - third * 10 - fourth;
    		while (d != 0 || second < 9) {
    			third = 0;
    			fourth = 0;
    			second++;
    			d = Math.pow(first,second) + Math.pow(third,fourth) - first * 1000 - second * 100 - third * 10 - fourth;
    			while (d != 0 || third < 9) {
    				fourth = 0;
    				third++;
    				d = Math.pow(first,second) + Math.pow(third,fourth) - first * 1000 - second * 100 - third * 10 - fourth;
    				while (d != 0 || fourth < 9) {
    					fourth++;
    					d = Math.pow(first,second) + Math.pow(third,fourth) - first * 1000 - second * 100 - third * 10 - fourth;
    				}
    			}
    		}

This is my current version. I fixed some of the boolean in the while loops, and I had to change int d; to double d; because compiler gave me errors saying that it's expecting a double.
Also, can somebody with a fast computer give this a twirl? Mine is quite old.

Edited 6 Years Ago by kbar: n/a

This article has been dead for over six months. Start a new discussion instead.