Check whether a number can be represented by sum of two squares | Javascript program to Check whether a number can be represented by sum of two squares using Fermat Theorem . ; Count all the prime factors . ; If any prime factor of the form ( 4 k + 3 ) ( 4 k + 3 ) occurs an odd number of times . ; If n itself is a prime number and can be expressed in the form of 4 k + 3 we return false . ; Driver Code
function judgeSquareSum ( n ) { for ( i = 2 ; i * i <= n ; i ++ ) { var count = 0 ; if ( n % i == 0 ) { while ( n % i == 0 ) { count ++ ; n = parseInt ( n / i ) ; } if ( i % 4 == 3 && count % 2 != 0 ) return false ; } } return n % 4 != 3 ; } var n = 17 ; if ( judgeSquareSum ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;