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
< ? php $ set = array ( ) ; $ prime = array ( ) ; function isPrime ( $ x ) { $ sqroot = sqrt ( $ x ) ; $ flag = true ; if ( $ x == 1 ) return false ; for ( $ i = 2 ; $ i <= $ sqroot ; $ i ++ ) if ( $ x % $ i == 0 ) return false ; return true ; } function display ( ) { global $ set , $ prime ; $ length = count ( $ set ) ; for ( $ i = 0 ; $ i < $ length ; $ i ++ ) echo ( $ set [ $ i ] . " ▁ " ) ; echo ( " STRNEWLINE " ) ; } function primeSum ( $ total , $ N , $ S , $ index ) { global $ set , $ prime ; if ( $ total == $ S && count ( $ set ) == $ N ) { display ( ) ; return ; } if ( $ total > $ S || $ index == count ( $ prime ) ) return ; array_push ( $ set , $ prime [ $ index ] ) ; primeSum ( $ total + $ prime [ $ index ] , $ N , $ S , $ index + 1 ) ; array_pop ( $ set ) ; primeSum ( $ total , $ N , $ S , $ index + 1 ) ; } function allPrime ( $ N , $ S , $ P ) { global $ set , $ prime ; for ( $ i = $ P + 1 ; $ i <= $ S ; $ i ++ ) { if ( isPrime ( $ i ) ) array_push ( $ prime , $ i ) ; } if ( count ( $ prime ) < $ N ) return ; primeSum ( 0 , $ N , $ S , 0 ) ; } $ S = 54 ; $ N = 2 ; $ P = 3 ; allPrime ( $ N , $ S , $ P ) ; ? >