(Hello, this is my first post here so I hope I do a good job of laying it out. I am happy to clarify or clean up examples if it might help out.)
Consider a table of numbers (n - horizontal axis) and all potential factors (f - vertical), with modular remainders n % f laid out on a grid as follows:
It's easy to visualise how each factor contributes to the emergence of prime numbers (marked P) - just find the vertical columns where the count of 0s = 2.
I was trying to understand how modular remainders relate to prime distances, as I've been trying to better understand the cause and limits of prime gaps.
Clearly the remainders in the above form only ever show the distance from previous iterations of each factor, so are useless for looking ahead.
That made me wonder what would happen if I negated n, such that the below table shows factors of -n % f:
In doing so, remainders now effectively represent the distances to "future" cycles of a given factor, where the "future" is toward the left of the table.
A pattern caught my eye in regards to prime gaps. For example if you look at the selected cells above, the lowest missing modular remainder when n=-7 is 4, which is the distance to the next prime number (albeit negative), -11.
Intuitively this makes sense; there won't be an explicit distance to the next prime number in any of the remainders of -n % f, because the next prime doesn't "exist" yet. But for any given n, the first missing remainder of -n % f is exactly the count to the next prime.
I wrote some quick and dirty code and tested successfully for tens of thousands of consecutive primes and a few relatively large ones (relative to a home PC running any kind of O(n^2) algorithm I guess). A cut down example is below:
https://jsfiddle.net/alexofparker/v0hg1r6x/13/
(I do have an optimised version that only requires mods of -n % 2, -n % 3 etc, which does the same thing many times faster, but is less obvious)
Is this way of calculating the prime gap significant in any way?
Conventional wisdom from what I've read and understood so far seems to be that there's no general & deterministic way to find the distance to the next prime number that doesn't involve counting up one number at a time from some start point and running a prime factorisation on each candidate.
I understand that various sieves both ancient and modern can vastly speed that process up as we can skip over a majority of repeating factor's cycles (e.g. for a period of 60).
But as I understand, sieves are non-deterministic; a sieve won't be able to tell you that the next prime number is exactly x digits from n.
Whereas although fairly clunky, it appears that finding the missing modular remainder of all potential factors of negative n can tell us the exact distance from any n to the next prime number.
So I guess, is what specific area in mathematics can teach me more about this pattern I've stumbled on - is there anything deeper here I can learn from and extend my knowledge? Am I just overthinking something elementary, or what am I missing?
Appreciate any and all insights into these questions!

