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
def sum_of_4_squares ( n ) : NEW_LINE INDENT i , odd , even = 0 , 0 , 0 NEW_LINE for i in range ( 1 , int ( n ** ( .5 ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT even += i NEW_LINE DEDENT else : NEW_LINE INDENT odd += i NEW_LINE DEDENT if ( ( n // i ) != i ) : NEW_LINE INDENT if ( ( n // i ) % 2 == 0 ) : NEW_LINE INDENT even += ( n // i ) NEW_LINE DEDENT else : NEW_LINE INDENT odd += ( n // i ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( n % 2 == 1 ) : NEW_LINE INDENT return 8 * ( odd + even ) NEW_LINE DEDENT else : NEW_LINE INDENT return 24 * ( odd ) NEW_LINE DEDENT DEDENT n = 4 NEW_LINE print ( sum_of_4_squares ( n ) ) NEW_LINE