# JavaHow do I solve this Java riddle?

#### deranged

##### New Coder
I have the following riddle: How do I solve this in Java, using while loops? I had an idea on how to do it with for-loops, but it doesn't work using for-loops
So far, all I have is the following, which seems to be going through all possible combinations, but is incredibly slow, so I suspect I made some mistake there...

Java:
``````public static void main(String[] args) {
int star = 0, delta = 0, diamond = 0, heart = 0, nabla = 0, square = 0, circleDot = 0, cross = 0, circleX = 0,
circle = 0;

int equation1 = getValue(star, delta, diamond) + getValue(heart, delta, nabla);
int result1 = getValue(nabla, square, square);
int equation2 = getValue(heart, nabla, circleDot) + getValue(square, square, star);
int result2 = getValue(cross, square, circleX);
int equation3 = getValue(nabla, square, square) - getValue(cross, square, circleX);
int result3 = getValue(heart, nabla, circle);

// Brute force solution with while loop
while (star < 10) {
while (delta < 10) {
while (diamond < 10) {
while (heart < 10) {
while (nabla < 10) {
while (square < 10) {
while (circleDot < 10) {
while (cross < 10) {
while (circleX < 10) {
while (circle < 10) {
if (equation1 == result1 && equation2 == result2
&& equation3 == result3) {
System.out.println("Star: " + star + ", Delta: " + delta
+ ", Diamond: " + diamond + ", Heart: " + heart
+ ", Nabla: " + nabla + ", Square: " + square
+ ", CircleDot: " + circleDot + ", Cross: " + cross
+ ", CircleX: " + circleX + ", Circle: " + circle);
}
circle++;
}
circle = 0;
circleX++;
}
circleX = 0;
cross++;
}
cross = 0;
circleDot++;
}
circleDot = 0;
square++;
}
square = 0;
nabla++;
}
nabla = 0;
heart++;
}
heart = 0;
diamond++;
}
diamond = 0;
delta++;
}
delta = 0;
star++;
}
}

public static int getValue(int a, int b, int c) {
int result = a * 100 + b * 10 + c;
return result;
}``````

Can someone please tell me how to solve this, I am getting crazy trying to...

I have the following riddle: How do I solve this in Java, using while loops? I had an idea on how to do it with for-loops, but it doesn't work using for-loops
So far, all I have is the following, which seems to be going through all possible combinations, but is incredibly slow, so I suspect I made some mistake there...

Java:
``````public static void main(String[] args) {
int star = 0, delta = 0, diamond = 0, heart = 0, nabla = 0, square = 0, circleDot = 0, cross = 0, circleX = 0,
circle = 0;

int equation1 = getValue(star, delta, diamond) + getValue(heart, delta, nabla);
int result1 = getValue(nabla, square, square);
int equation2 = getValue(heart, nabla, circleDot) + getValue(square, square, star);
int result2 = getValue(cross, square, circleX);
int equation3 = getValue(nabla, square, square) - getValue(cross, square, circleX);
int result3 = getValue(heart, nabla, circle);

// Brute force solution with while loop
while (star < 10) {
while (delta < 10) {
while (diamond < 10) {
while (heart < 10) {
while (nabla < 10) {
while (square < 10) {
while (circleDot < 10) {
while (cross < 10) {
while (circleX < 10) {
while (circle < 10) {
if (equation1 == result1 && equation2 == result2
&& equation3 == result3) {
System.out.println("Star: " + star + ", Delta: " + delta
+ ", Diamond: " + diamond + ", Heart: " + heart
+ ", Nabla: " + nabla + ", Square: " + square
+ ", CircleDot: " + circleDot + ", Cross: " + cross
+ ", CircleX: " + circleX + ", Circle: " + circle);
}
circle++;
}
circle = 0;
circleX++;
}
circleX = 0;
cross++;
}
cross = 0;
circleDot++;
}
circleDot = 0;
square++;
}
square = 0;
nabla++;
}
nabla = 0;
heart++;
}
heart = 0;
diamond++;
}
diamond = 0;
delta++;
}
delta = 0;
star++;
}
}

public static int getValue(int a, int b, int c) {
int result = a * 100 + b * 10 + c;
return result;
}``````

Can someone please tell me how to solve this, I am getting crazy trying to...

There are 6 equations, 3 horizontal and 3 vertical.

Only 3 loop variables are needed because the values of the other 7 variables can be calculated.

For example, consider the least significant digits of this equation,

ABC + DBE = EFF

C + E is equivalent to F (modulo 10)

If 2 of {C,E,F} are known, the unknown variable can be calculated.

I'm just going to chime in with a warning in regards to your code.

Do not nest your loops that deep! It makes your code more difficult to fix if any errors arise and it will potentially slow down your program too and any calculations it needs to make.

Yeah, that will be slow... You don't break when the solution is found so the total number of iterations will be 10 to the power of 10. Maybe in a fully compiled language this is not such a big deal but in Java it definitely will be. Are you required to find just one solution ? What we have here is 6 equations with 10 variables. One of the things I remember from my math time is that you need to have as many equations as variables. So I'd not be surprised if there would be thousands of solutions. Did you let your code run to the end ? Did you get any output lines ? How many ?

The real clonker in your code is that the values of `equation1`, `result1`, etc are only evaluated once, at the very start when all variables are zero. So all `getValue()` calls return zero too. You need to evaluate them each time you get to the inner core of your nested loop ! Making it much slower still..... Also, you need to test the vertical equations (which you seem to have missed) as well as the horizontal equations ! Making it even slower still...

I have a feeling there must be a smarter way to go about this than using brute force, although I honestly don't know what that would be. Perhaps apply some savvy math analysis first. Then again if you are only required to produce one solution perhaps this approach is viable. Provided you do break your loops when you hit the jackpot 😁

Cosmetically, what I'd have done is replace these symbols by letters `a..j`, use Allman indentation and a smaller tab size. That makes the code much more palatable IMO. Although it is a matter of taste.

Java:
``````public static void main(String[] args)
{
int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0;

int equ1 = getValue(a, b, c) + getValue(d, b, e);
int res1 = getValue(e, f, f);
int equ2 = getValue(d, e, g) + getValue(f, f, a);
int res2 = getValue(h, f, i);
int equ3 = getValue(e, f, f) - getValue(h, f, i);
int res3 = getValue(d, e, j);

while (a < 10)
{
while (b < 10)
{
while (c < 10)
{
while (d < 10)
{
while (e < 10)
{
while (f < 10)
{
while (g < 10)
{
while (h < 10)
{
while (i < 10)
{
while (j < 10)
{
if (equ1 == res1 && equ2 == res2 && equ3 == res3)
{
System.out.println(
"  a:" + a + ", b:" + b
+ ", c:" + c + ", d:" + d
+ ", e:" + e + ", f:" + f
+ ", g:" + g + ", h:" + h
+ ", i:" + i + ", j:" + j);
}
j++;
}
j = 0;
i++;
}
i = 0;
h++;
}
h = 0;
g++;
}
g = 0;
f++;
}
f = 0;
e++;
}
e = 0;
d++;
}
d = 0;
c++;
}
c = 0;
b++;
}
b = 0;
a++;
}
}

public static int getValue(int a, int b, int c)
{
int result = a * 100 + b * 10 + c;
return result;
}``````

Last edited by a moderator:
Code:
``````abc + dbe = eff

-     -     -

deg + ffa = hfi

=     =     =

cbi + fed = dej``````   