Number of ways of writing N as a sum of 4 squares | Number of ways of writing n as a sum of 4 squares ; Sum of odd and even factor ; Iterate from 1 to the number ; If i is the factor of n ; If factor is even ; If factor is odd ; n / i is also a factor ; If factor is even ; If factor is odd ; If n is odd ; If n is even ; Driver code
function sum_of_4_squares ( n ) { var i , odd = 0 , even = 0 ; for ( i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i % 2 == 0 ) even += i ; else odd += i ; if ( ( n / i ) != i ) { if ( ( n / i ) % 2 == 0 ) even += ( n / i ) ; else odd += ( n / i ) ; } } } if ( n % 2 == 1 ) return 8 * ( odd + even ) ; else return 24 * ( odd ) ; } var n = 4 ; document . write ( sum_of_4_squares ( n ) ) ;