Prime numbers after prime P with sum S | vector to store prime and N primes whose sum equals given S ; function to check prime number ; square root of x ; since 1 is not prime number ; if any factor is found return false ; no factor found ; function to display N primes whose sum equals S ; function to evaluate all possible N primes whose sum equals S ; if total equals S And total is reached using N primes ; display the N primes ; if total is greater than S or if index has reached last element ; add prime [ index ] to set vector ; include the ( index ) th prime to total ; remove element from set vector ; exclude ( index ) th prime ; function to generate all primes ; all primes less than S itself ; if i is prime add it to prime vector ; if primes are less than N ; Driver Code
var set = [ ] ; var prime = [ ] ; function isPrime ( x ) { var sqroot = Math . sqrt ( x ) ; var flag = true ; if ( x == 1 ) return false ; for ( var i = 2 ; i <= sqroot ; i ++ ) if ( x % i == 0 ) return false ; return true ; } function display ( ) { var length = set . length ; for ( var i = 0 ; i < length ; i ++ ) document . write ( set [ i ] + " " ) ; document . write ( " " ) ; } function primeSum ( total , N , S , index ) { if ( total == S && set . length == N ) { display ( ) ; return ; } if ( total > S index == prime . length ) return ; set . push ( prime [ index ] ) ; primeSum ( total + prime [ index ] , N , S , index + 1 ) ; set . pop ( ) ; primeSum ( total , N , S , index + 1 ) ; } function allPrime ( N , S , P ) { for ( var i = P + 1 ; i <= S ; i ++ ) { if ( isPrime ( i ) ) prime . push ( i ) ; } if ( prime . length < N ) return ; primeSum ( 0 , N , S , 0 ) ; } var S = 54 , N = 2 , P = 3 ; allPrime ( N , S , P ) ;