AKS Primality Test | array used to store coefficients . ; function to calculate the coefficients of ( x - 1 ) ^ n - ( x ^ n - 1 ) with the help of Pascal 's triangle . ; function to check whether the number is prime or not ; Calculating all the coefficients by the function coef and storing all the coefficients in c array . ; checking all the coefficients whether they are divisible by n or not . if n is not prime , then loop breaks and ( i > 0 ) . ; Return true if all coefficients are divisible by n . ; Driver Code
< ? php global $ c ; function coef ( $ n ) { $ c [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ n ; $ c [ 0 ] = - $ c [ 0 ] , $ i ++ ) { $ c [ 1 + $ i ] = 1 ; for ( $ j = $ i ; $ j > 0 ; $ j -- ) $ c [ $ j ] = $ c [ $ j - 1 ] - $ c [ $ j ] ; } } function isPrime ( $ n ) { global $ c ; coef ( $ n ) ; $ i = $ n ; while ( $ i -- && $ c [ $ i ] % $ n == 0 ) return $ i < 0 ; } $ n = 37 ; if ( isPrime ( $ n ) ) echo " Not ▁ Prime " , " STRNEWLINE " ; else echo " Prime " , " STRNEWLINE " ; ? >