This is a problem proposed by Vietnamese for a 3rd-grade student international contest in Malaysia, 2017. You have to find 3 numbers with conditions:
[][][][] - [][][][] - [] = 2017.
Each digit in each cell range from 1 to 9 and they must be different each other.
I can't solve by hand, so I have made a program to find results. My code works but is too long, so does anyone have a shorter way?
public class Third_gradeProblem {
public static void main(String[] args) {
int a,b,c,d,e,f,g,h,i;
int so1,so2,so3;
for(a=1;a<=9;a++) {
for(b=1;b<=9;b++) {
for(c=1;c<=9;c++) {
for(d=1;d<=9;d++) {
for(e=1;e<=9;e++) {
for(f=1;f<=9;f++) {
for(g=1;g<=9;g++) {
for(h=1;h<=9;h++) {
for(i=1;i<=9;i++) {
so1=a*1000+b*100+c*10+d;
so2=e*1000+f*100+g*10+h;
so3=i;
if((so1-so2-so3==2017)&&(a!=b)&&(a!=c)&&
(a!=d)&&(a!=e)&&(a!=f)&&(a!=g)&&(a!=h)&&(a!=i)&&(b!=c)&&(b!=d)&&(b!=e)&&
(b!=f)&&(b!=g)&&(b!=h)&&(b!=i)&&(c!=d)&&(c!=e)&&(c!=f)&&(c!=g)&&(c!=h)&&
(c!=i)&&(d!=e)&&(d!=f)&&(d!=g)&&(d!=h)&&(d!=i)&&(e!=f)&&(e!=g)&&(e!=h)&&
(e!=i)&&(f!=g)&&(f!=h)&&(f!=i)&&(g!=h)&&(g!=i)&&(h!=i)) {
System.out.println(so1+" "+so2+"
"+so3);
}
}
}
}
}
}
}
}
}
}
}
}
Results:
4613 2587 9;
4613 2589 7;
7412 5386 9;
7412 5389 6;
7421 5396 8;
7421 5398 6;