Is four integers a constraint? 'Cause I've come across some test cases where the numbers are getting represented by more than four integers for ex. 2456.
Or, is there any constraint on the number to be represented?
EDIT:
This is the best I could come up with :
Code:
class sum{
public static int prod(int num){
int i=0,n;
/* if(num==0)
return 0; */
while(true){
if((i*i)>num){
i--;
n=i*i;
break;
}
i++;
}
//System.out.print(" "+n);
//return prod(num-n);
return n;
}
public static void main(String[] args){
int num=Integer.parseInt(args[0]);
System.out.println(num + " = ");
for(int count=0;count<4;count++){
int n=prod(num);
int sqroot=(int)Math.sqrt(n);
System.out.println(sqroot +" * "+ sqroot +" = " + n);
if(n==1)
continue;
num=num-n;
}
//System.out.println(num + " " + n);
}
}
I've tried to make it recursive, but stopped to keep it simple (The commented parts were for recursion). This code works perfectly for most test cases (Ex. 1234), but gives extra integers for some test cases (Ex. 2456).
This is what I get for 78 :
Code:
78 =
8 * 8 = 64
3 * 3 = 9
2 * 2 = 4
1 * 1 = 1