Possible Duplicate:
Algorithm to find Lucky Numbers
I came across this question.A number is called lucky if the sum of its digits, as well as the sum of the squares of its digits is a prime number. How many numbers between A and B are lucky? 1 <= A <= B <= 10^18.
I tried this,
First I generated all possible primes between 1 and the number that could be resulted by summing the squares (81 *18 = 1458).[Note: I used sieve of Atkin's approach for prime number generation].
And then verifying each number's digit sum and digit square sum is in the list of primes, if so it is lucky otherwise not. But this is very very slow. Is there any better way of solving?