Given two number a and b (1 <= a <= b <= 10^6). Find most frequent digit among all prime numbers between a and b inclusive. if frequency is same print highest digit.
Example: from 1 to 20, prime numbers are - 2, 3, 5, 7, 11, 13, 17, 19. Here 2, 5, 9 occur only once, 3, 7 occur twice, and 1 occurs 5 times. So the result is 1.
One basic approach is:
- In the range [a, b] - find all prime numbers.
- Take one count array to count occurrences from 0 to 9.
- For all the prime numbers in the range, extract all the digits and increment the count for the digits accordingly in the count array.
- Find out max from the count array.
But this is inefficient for a large range say [1, 1000000].
Is there any efficient method to accomplish this?