text: Insert minimum number in array so that sum of array becomes prime | function to check if a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Find prime number greater than a number ; find prime greater than n ; check if num is prime ; increment num ; To find number to be added so sum of array is prime ; To find sum of array elements ; if sum is already prime return 0 ; To find prime number greater than sum ; Return difference of sum and num ; Driver Code
code: < ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ n % $ i == 0 ) return false ; return true ; } function findPrime ( $ n ) { $ num = $ n + 1 ; while ( $ num ) { if ( isPrime ( $ num ) ) return $ num ; $ num = $ num + 1 ; } return 0 ; } function minNumber ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; if ( isPrime ( $ sum ) ) return 0 ; $ num = findPrime ( $ sum ) ; return $ num - $ sum ; } $ arr = array ( 2 , 4 , 6 , 8 , 12 ) ; $ n = sizeof ( $ arr ) ; echo minNumber ( $ arr , $ n ) ; ? >