Three Wise Men (Java)

Question is from here:

We have a simple Christmas puzzle today:

Three wise men named Caspar, Melchior and Balthazar went to Macy’s Department Store on 34th Street to buy gifts of gold, frankincense, and myrrh. When they took their gifts to the cashier, she multiplied the prices of the items and found a total price of $65.52, but the wise men noticed that she multiplied the numbers and asked her to compute the total price again, but this time using addition instead of multiplication. When she did, the cashier found that the total was exactly the same. What were the individual prices of the three gifts?

Your task is to solve the puzzle and find the three prices.
Solution: it is based on brute force, as soon as it finds a solution, it will stop the program and print out x, y and z.

public class Three_Wise_Men {
	public static void main(String[] args) {
		solution();
	}

	public static void solution(){
		int ans = 6552;
		outerLopp:
		for(float x = 1; x < 6552; x++) {
			for(float y = 1; y < 6552; y++) {
				for(float z = 1; z < 6552; z++) {
					if(x+y+z == x*y*z/10000 && x+z+y == ans){
						System.out.println("x: " + x/100 +
								"   y: " + y/100 + "   z: "+ z/100);
						break outerLopp;
					}
				}
			}
		}
	}
}

Output:

x: 0.52   y: 2.0   z: 63.0
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s