text: Check if the first and last digit of the smallest number forms a prime | function to check prime ; Function to generate smallest possible number with given digits ; Declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; Traverse the hash in ascending order to print the required number ; Print the number of times a digits occurs ; extracting the first digit ; extracting the last digit ; printing the prime combinations ; Driver Code
code: < ? php function isPrime ( $ n ) { $ c = 0 ; for ( $ i = 1 ; $ i < $ n / 2 ; $ i ++ ) { if ( $ n % $ i == 0 ) $ c ++ ; } if ( $ c == 1 ) return 1 ; else return 0 ; } function findMinNum ( $ arr , $ n ) { $ first = 0 ; $ last = 0 ; $ num ; $ rev ; $ i ; $ hash = array_fill ( 0 , 20 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ hash [ $ arr [ $ i ] ] ++ ; } echo " Minimum ▁ number : ▁ " ; for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ hash [ $ i ] ; $ j ++ ) echo $ i ; } for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { if ( $ hash [ $ i ] != 0 ) { $ first = $ i ; break ; } } for ( $ i = 9 ; $ i >= 0 ; $ i -- ) { if ( $ hash [ $ i ] != 0 ) { $ last = $ i ; break ; } } $ num = $ first * 10 + $ last ; $ rev = $ last * 10 + $ first ; echo " Prime combinations : " if ( isPrime ( $ num ) && isPrime ( $ rev ) ) echo $ num . " ▁ " . $ rev ; else if ( isPrime ( $ num ) ) echo $ num ; else if ( isPrime ( $ rev ) ) echo $ rev ; else echo " No ▁ combinations ▁ exist " ; } $ arr = array ( 1 , 2 , 4 , 7 , 8 ) ; findMinNum ( $ arr , 5 ) ; ? >