sum of four squares of an integer

vickybat

I am the night...I am...
^^ Did you check that algorithm i posted given by that guy? I think that's a good solution coz it finds multiple possibilities.
 

Desmond

Destroy Erase Improve
Staff member
Admin
^^ Have you added keyboard input? How are you giving input?

I'm getting the following error:

Code:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	n cannot be resolved to a variable

	at sum.main(sum.java:32)

Sorry, it takes input as an argument.
 

icebags

Technomancer
^^ Did you check that algorithm i posted given by that guy? I think that's a good solution coz it finds multiple possibilities.

now i checked.
the guy takes that assumption, but didn't explain why this logic must work. anyways, thanks for the link.

if u want this way, try this ::
Code:
z = input val;

i1 = floor(sqrt(z));                        --> like floor of sqrt(10) is 3
i2 = floor( Sqrt(z - i1^2) );
i3 = floor( Sqrt(z - i1^2 - i2^2) );
i4 =  Sqrt(z - i1^2 - i2^2 - i3^2 ) ;
now, as per that logic, its should be : z = i1^2 + i2^2 + i3^2 + i4^2.

hopefully i didn't make mistake.


36^2 + 34^2 + 2^2 + 0^2 = 2456

No one read my links :x

big huge program and i am bad at mathematics. (it kinda ruined my HS result). sorry man. :(.
 
Last edited:

vickybat

I am the night...I am...
^^ Will try out ur logic mate.

This works i guess. Algorithm isn't mine and i found it over web.

Check it out guys:

Code:
[B]public class lagrange{
	public static void sum_of_squares(int n){
	int t1, t2, t;
   
       for (int i = (int) Math.sqrt(n / 4); i * i <= n; i++) {
       
               t1 = n - i * i;
     
       for (int j = (int) Math.sqrt(t1 / 3); j <= i && j * j <= t1; j++) {
            
               t2 = t1 - j * j;
            
       for (int k = (int) Math.sqrt(t2 / 2); k <= j && k * k <= t2; k++) {
               
                t = (int) Math.sqrt(t2 - k * k);
                
           if (t <= k && t * t == t2 - k * k) {
                    System.out.println("(" + i + "^2) + (" + j + "^2) + ("+ k + "^2) + ("+ t +"^2)");
                }
            }
        }
    }
}
            
        
        public static void main (String [] args){
        	sum_of_squares(29);
        
        }
    }
[/B]

Pass any arguements into the sum_of_squares method. It also gives multiple possibilities. Numbers like 23 won't work coz it requires 5 terms
i.e 4^2 + 2^2+ 1^2+ 1^2+ 1^2

Algorithm for that is different. Interestingly, all counter variables used, yield the answer.
 
Last edited:
Top Bottom