I have to write a program such that can determine the number of ordered pairs of prime numbers (p, q) such that when N = p^2+q^3 is written in decimal, each digit from 0 to 9 appears exactly once (without leading zeroes).
I thought of using a variation of a sieve of Eratosthenes as it's explained here, however it's also mentioned that a sieve only works for numbers N such that N is less than ten million but by the statement of the problem each N is at least one hundred millions from the fact that each digit appears exactly once and there's no leading zeroes, but I don't have any other ideas apart from this one so any help would be appreciated.
Following the same technique as @Dave, but with a variable initial value for the inner loop, we get about 20% less iterations. I think this would be the optimal way. Here is an example using php, with access to 2 functions:
p()is the nth prime andpi()is the prime counting function.Output:
Note: the
max()function is to avoid errors with cubic root of negative numbers.