# Prime generating modular function

I found that

$$(671n \mod 2454) + (304n \mod 32) + (4373n \mod 199)$$

generates $$38$$ distinct primes for $$n=1$$ to $$38$$: $$881, 1531, 2213, 409, 1091, 1741, 2423, 619, 1301, 1951, 179, 829, 1511, 2161, 389, 1039, 1721, 2371, 599, 1249, 1931, 127, 809, 1459, 2141, 337, 1019, 1669, 2351, 547, 1229, 1879, 107, 757, 1439, 2089, 317, 967.$$

Is this anything unusual and is there a good explanation for it? Is there a function of the same form that generates more consecutive terms of distinct primes? The function must be of the form $$\sum_{i}(A_in \mod B_i)$$, where all $$A_i, B_i$$ are positive integers.

• I used hill climbing to find this formula. Each move consists of changing one of the 6 variables. I believe better formulas exist though. Commented Dec 17, 2020 at 10:46
• A kind of genetic algorithm. Well done ! I think, it will be hard to beat this, but I share your expectation that it can be beaten. Commented Dec 17, 2020 at 10:51
• The last two summands are simpler as $\ 16(n\bmod 2) + (-5n\bmod 199)\ \$ Commented Dec 17, 2020 at 14:21
• This modular function can also be written as: ((666n+199) mod 2454)+16(n mod 2). Written this way it will generate 41 distinct primes for n=-2 to 38 (primes for -2 to 0: 1321, 2003 and 199. All other values identical to the original function. Commented Dec 19, 2020 at 17:25
• @Peter we can of course change pietfermat's function so it starts with $n=1$ by subtracting 3 from $n$: $(666n-1799) \mod 2454+16((n-3) \mod 2)$. If you don't like minuses, this can be converted to $(666n+655) \mod 2454+16((n+1) \mod 2)$. Finally I did manage to find a function of the original form that produces 41 primes: $(n*639)\mod 558 + (n*355)\mod 344 + (n*1124)\mod 1066 + (n*1294)\mod1230 + (n*175)\mod 179$. Commented Dec 22, 2020 at 0:15