Check if a number can be represented as a sum of a Prime Number and a Perfect Square | Function to store all prime numbers less than or equal to N ; Update prime [ 0 ] and prime [ 1 ] as false ; Iterate over the range [ 2 , sqrt ( N ) ] ; If p is a prime ; Update all multiples of p which are <= n as non - prime ; Function to check whether a number can be represented as the sum of a prime number and a perfect square ; Stores all the prime numbers less than or equal to n ; Update the array prime [ ] ; Iterate over the range [ 0 , n ] ; If current number is non - prime ; Update difference ; If difference is a perfect square ; If true , update flag and break out of loop ; If N can be expressed as sum of prime number and perfect square ; Driver Cod
function SieveOfEratosthenes ( prime , n ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } function sumOfPrimeSquare ( n ) { let flag = false ; let prime = new Array ( n + 1 ) . fill ( true ) ; SieveOfEratosthenes ( prime , n ) ; for ( let i = 0 ; i <= n ; i ++ ) { if ( ! prime [ i ] ) continue ; let dif = n - i ; if ( Math . ceil ( Math . sqrt ( dif ) ) == Math . floor ( Math . sqrt ( dif ) ) ) { flag = true ; break ; } } if ( flag ) { document . write ( " " ) ; } else document . write ( " " ) ; } let N = 27 ; sumOfPrimeSquare ( N ) ;