Make precomputation in range 1..1000000 of all primes and sum of their digits using sieve of eratosthenes. Save in boolean array which of them is digit prime or not. Then precompute in array sum[1000001] number of digit primes, such that sum = number of digit primes between 1 and i. Then process input. Using this array you can get answer in O(1). For each input t1 and t2 answer is sum[t2] - sum[t1 - 1].
![:)](./images/smilies/icon_smile.gif)
By the way, sum of digits of a number can be also calculated using sprintf without making division and mod operations.