I have a search and test problem : in the list of prime numbers from 2 to 100k, we're searching the first set of 5 with the following criteria :
- p1 < p2 < p3 < p4 < p5
- any combination of 2 primes from the solution (3 and 7 => 37 and 73) must also be a prime
- sum(p1..p5) is the smallest possible sum of primes satisfying criteria, and is above 100k
I can totally code such a thing, but i have a severe optimization problem : my code is super duper slow. I have a list of primes under 100k, a list of primes over 100k, and a primality test which works well, but i do not see how to optimize that to obtain a result in a correct time.
For a basic idea :
- the list of all primes under 100k contains 9592 items
- the list of all primes under 1 billion contains approximately 51 million lines
- i have the list of all primes under 1 billion, by length
Thanks for the help
Here is a numba version that computes the minimal combination of the prime numbers with restrictions as stated in the question.
The majority of runtime is spent in pre-computing all valid combinations of prime numbers. On my computer (AMD 5700X) this runs in 1 minute 20 seconds:
Prints: