I often need to generate or run through very large numbers of integers (we're talking {i, 1, 10^9} and higher) and want to add certain integers to a list, say list1.
So far what I've been doing is use the following structure:
list1 = {};
Do[If[PrimeQ[i] && Sort[IntegerDigits[i]] == Range[9], AppendTo[list1, i]], {i, 10^9}];
list1
(This is an example of a function that adds to list1 only those values that are prime as well as pandigital from 1 to 9)
Now I realise that this is probably a horribly inefficient way to generate lists. How can I ensure that my Do only runs through those functions which fulfil my criteria, e.g. being prime, or being divisible by 14, and so forth, and skips those which it doesn't need to run through - especially when I am running through 1 billion values for i? Is this even possible? Won't my function by definition have to run through every value anyway?
9! == 362880pandigital numbers in this range andPrimePi[10^9] == 50847534primes. So I'd create a list of pandigital numbers, thenScanit withPrimeQandSow/Reapthe results. Why do you even try fori<10^8? – Kuba Mar 13 '14 at 06:18Reap@Do[If[Sort@IntegerDigits@Prime[i] == Range[9], Sow@Prime[i]], {i,PrimePi[10^8] + 1, PrimePi[10^9]}]– Szabolcs Mar 13 '14 at 16:30