Find n | PHP program to find n - th Fortunate number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to Find primorial of order n ( product of first n prime numbers ) . ; Function to find next prime number greater than n ; Note that difference ( or m ) should be greater than 1. ; loop continuously until isPrime returns true for a number above n ; Ignoring the prime number that is 1 greater than n ; Returns n - th Fortunate number ; Driver Code
< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } function primorial ( $ n ) { $ p = 2 ; $ n -- ; for ( $ i = 3 ; $ n != 0 ; $ i ++ ) { if ( isPrime ( $ i ) ) { $ p = $ p * $ i ; $ n -- ; } $ i ++ ; } return $ p ; } function findNextPrime ( $ n ) { $ nextPrime = $ n + 2 ; while ( true ) { if ( isPrime ( $ nextPrime ) ) break ; $ nextPrime ++ ; } return $ nextPrime ; } function fortunateNumber ( $ n ) { $ p = primorial ( $ n ) ; return findNextPrime ( $ p ) - $ p ; } $ n = 5 ; echo fortunateNumber ( $ n ) , " STRNEWLINE " ; ? >