Count of ways to represent N as sum of a prime number and twice of a square | JavaScript implementation to count the number of ways a number can be written as sum of prime number and twice a square ; Function to mark all the prime numbers using sieve ; Loop to mark the prime numbers upto the Square root of N ; Loop to store the prime numbers in an array ; Function to find the number ways to represent a number as the sum of prime number and square of a number ; Loop to iterate over all the possible prime numbers ; Increment the count if the given number is a valid number ; Driver Code ; Function Call
let n = 500000 - 2 ; let v = [ ] ; function sieveoferanthones ( ) { let prime = Array . from ( { length : n + 1 } , ( _ , i ) => true ) ; for ( let i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( prime [ i ] ) for ( let j = i * i ; j <= n ; j += i ) { prime [ j ] = false ; } } for ( let i = 2 ; i < n ; i ++ ) { if ( prime [ i ] ) v . push ( i ) ; } } function numberOfWays ( n ) { let count = 0 ; for ( let j = 1 ; 2 * ( Math . pow ( j , 2 ) ) < n ; j ++ ) { for ( let i = 1 ; v [ i ] + 2 <= n ; i ++ ) { if ( n == v [ i ] + ( 2 * ( Math . pow ( j , 2 ) ) ) ) count ++ ; } } document . write ( count + " " ) ; } sieveoferanthones ( ) ; let N = 9 ; numberOfWays ( N ) ;