Find the highest occurring digit in prime numbers in a range | Sieve of Eratosthenes ; Returns maximum occurring digits in primes from l to r . ; Finding the prime number up to R . ; Initialise frequency of all digit to 0. ; For all number between L to R , check if prime or not . If prime , incrementing the frequency of digits present in the prime number . ; $p = $i ; If i is prime ; Finding digit with highest frequency . ; Driver Code
< ? php function sieve ( & $ prime , $ n ) { for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == false ) for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = true ; } } function maxDigitInPrimes ( $ L , $ R ) { $ prime = array_fill ( 0 , $ R + 1 , false ) ; sieve ( $ prime , $ R ) ; $ freq = array_fill ( 0 , 10 , 0 ) ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { if ( ! $ prime [ $ i ] ) { while ( $ p ) { $ freq [ $ p % 10 ] ++ ; $ p = ( int ) ( $ p / 10 ) ; } } } $ max = $ freq [ 0 ] ; $ ans = 0 ; for ( $ j = 1 ; $ j < 10 ; $ j ++ ) { if ( $ max <= $ freq [ $ j ] ) { $ max = $ freq [ $ j ] ; $ ans = $ j ; } } return $ ans ; } $ L = 1 ; $ R = 20 ; echo maxDigitInPrimes ( $ L , $ R ) ; ? >