NHacker Next
login
▲Show HN: Prime Number Grid Visualizerenda.sh
95 points by dduplex 3 days ago | 39 comments
Loading comments...
kawfey 12 hours ago [-]
I spammed the columns from 1 to 402 (with 400 rows), and i saw some cool patterns. I can see a few nodes of clockwise rotation that converges into a single node from >200 columns that starts makes its way down the screen. Trippy.

I made a screen recording of it: https://n0ssc.com/wp-content/uploads/2025/08/prime-doublespe...

HocusLocus 8 hours ago [-]
Rotation effects of negative (whitespace) of moiré patterns from multiples of [2,3,5,7,11,13,17,19]. With each increment they are tilting at different 'rates' so the whole represents rotation of straight artifacts at several speeds at once.
clueless 9 hours ago [-]
woah, yeah, what is up with the galaxies spin motion/transformation? there's gotta be a good explanation
AnotherGoodName 8 hours ago [-]
Start with the vertical patterns; The vertical patterns happen for any composite number. Take the number 10 for example. Obviously numbers 10n + [0,2,4,6,8] are divisible by 2 and 10n + 5 are divisible by 5. So you can understand the vertical gaps in primality whenever you display a composite number. Multiple's of 10 is one you can probably grasp quickly but there's nothing special about base 10.

Now when you take a vertical pattern like you see with column width=10 and add one more to the column size (so make it 11) the pattern is now offset by one pixel on each row. This makes the vertical pattern render diagonally as each row is offset by a pixel.

So you get the patterns going between vertical and diagonal. This can get really interesting when you have a bunch of composite numbers in a row. Each composite number has it's own vertical pattern as described above. So 435 has gaps every 3rd and 5th number (and for other factors) since those will be multiple of 3 or 5. Now the next number, 436 rotates this pattern 45degrees since it offsets each row by 1 pixel. But.. 436 has it's own patterns, eg. every second number is a multiple of 2 when offset from 436 since 436 is even. So it has it's own vertical patterns but also shows the diagonals from gaps in 435.

Anyway these well understood gaps in primality give a galaxy appearance for this simple reason. You're seeing well understood vertical patterns shifting to render diagonally with new vertical patterns from alignment on the next number and repeat.

jasonjmcghee 11 hours ago [-]
I did the same thing - but you can just put your cursor in the number box and hold up / down arrow key.
lurn_mor 11 hours ago [-]
Very! I saw the distant galaxies spin.
artemonster 11 hours ago [-]
I did the same thing and saw galaxies
physix 9 hours ago [-]
I was waiting for

          .....   ......   ......   ....   ..    ..   ......
         ..       ..   ..  ..   ..   ..    ...   ..   ..     
         ..       ..   ..  ...  ..   ..    .. .. ..   ..     
          .....   ......   ......    ..    .. .. ..   ..  ...
           .....  ...      .. ..     ..    ..   ...   ..   ..
              ..  ..       ..  ..    ..    ..    ..   ...  ..
         ......   ..       ..   ..  ....   ..    ..    .....

to show up, until I realized I've been coding too much today.
brandonasuncion 10 hours ago [-]
Reminds me of a really cool coding trick to get a "random" permutation of an array in O(1) time/memory.

https://lemire.me/blog/2017/09/18/visiting-all-values-in-an-...

teiferer 8 minutes ago [-]
That's neither random nor O(1). I think everybody reading the link immediately understands what you mean, but I whished this community would use its technical terminology more carefully.
yummypaint 8 hours ago [-]
This is very cool, it reminds me of an ulam spiral. https://en.m.wikipedia.org/wiki/Ulam_spiral
vismit2000 8 hours ago [-]
Reminds of https://www.3blue1brown.com/lessons/prime-spirals
throwmeaway222 7 hours ago [-]
Someone's going to find the correct column count to reveal the hidden message to build an intergalactic space ship with FTL travel.
yen223 6 hours ago [-]
A feature Id like to see is an option to invert the grid, i.e. show the composites instead of the primes
mfoc 9 hours ago [-]
When choosing rows = 4000 and columns = 546, an interesting pattern emerges.

For all integers n ≥ 0, the ranges [243 + (n * 546)] to [249 + (n * 546)] inclusive appear to contain no prime numbers. Same with the ranges [297 + (n * 546)] to [303 + (n * 546)].

For both sets of ranges, the minimum gap between the closest neighbouring primes appears to be at least 10 (in decimal). Does anyone know of a number-theoretic explanation for this kind of pattern?

Karliss 8 hours ago [-]
Nothing too surprising more or less same thing as all other vertical columns. 546=2*3*7*13 and all numbers in the range [242, 250] can be divided by one of those 4 primes same for [296, 304] . If x is divisor of a and b, then it will also divide a+n*b meaning you get an empty column. 2 and 3 already makes more than half the columns non primes, fill in the gaps with few more primes and you get the wide empty columns. If the width is multiple of many different small primes it's more likely to happen.

In a similar way 210=2*3*7*5 also gives wide empty columns (if you ignore ignore first row where 2,3,5,7 themselves are primes)

It helps if you think of it in terms of where the non primes are located instead of where the primes are. Multiples of 2, 3, 5 form a very regular pattern. Wrap it around in a grid and you get straight lines which are either straight vertical or slightly shifted depending on the divisors of width. Stack a couple of repetitive patterns and you still get a repetitive pattern. If the positions which are not primes form a regular pattern, the inverted image also forms recognizable pattern. Of course the primes don't form perfectly regular pattern and but most the visible repetition are result of small prime multiples.

mfoc 5 hours ago [-]
Thank you.
AnotherGoodName 8 hours ago [-]
Those are just columns that happen to share factors with 546 and happen to line up together. 546 is a very composite number (lots of factors!)

546n + 242 is always even

546n + 243 is always divisible by 3

546n + 244 is always even

546n + 255 is always divisible by 7.

Etc.

It's similar to how in base 10 you never see a prime ending in 0,2,4,6,8 or 5 since those numbers are clearly divisible by 2 or 5. In 546's case you have a lot of factors so even more gaps.

You also get the pattern appearing again since the number is even and the non-even factors repeat their pattern starting from the halfway point.

mfoc 8 hours ago [-]
That makes perfect sense. Thank you.

To finish it off (skipping even numbers)...

546n + 247 is always divisible by 13.

546n + 249 is always divisible by 3.

8 hours ago [-]
voisin 9 hours ago [-]
I am interested to know how you discovered this! Was it by happenstance or did you know to look for this?
mfoc 8 hours ago [-]
I had noticed it before. The response by AnotherGoodName makes it clear to me now why.
8bitsrule 8 hours ago [-]
It'd be great to be able to plot only the primes that satisfy some simple (at first anyways) math operations. "Show me the primes that when (math operation) by (a set of values) equals (set of results). EG when (mult,div,squared) by (n1,n2...) equals (0,e^2, ...). Exploring for surprise patterns!
heywire 6 hours ago [-]
Reminds me of what I do when trying to look for structure in binary data. Print it to the screen in either bits or bites with word wrap enabled and drag the window around.
noduerme 6 hours ago [-]
The trick is to do 88 columns, punch them out on a roll, and run it through a player piano.
navane 12 hours ago [-]
I'm probably an idiot but having the columns at a multiple of 6 is very pleasing
AnotherGoodName 10 hours ago [-]
The reason behind that is similar to how all prime numbers above 2 are of the form 2n+1 since all other numbers are divisible by 2. Eg. all prime numbers >2 are odd.

In this case you're seeing the extension of this to include multiples of 3. That is, all prime numbers above 6 are of the form 6n+1 or 6n+5, all other numbers are either divisible by 2 or 3.

You can extend these patterns. Whenever you have a composite number you'll get periodic points where factors are known. Eg. to extend it one step further you could say all prime numbers above 30 are of the form 30n + [1,7,11,13,17,19,23,29], all others are divisible by 2, 3 or 5.

From this you can also quickly iterate towards to the prime number formula for the frequency of primes. eg. Only half of numbers above 2 can be prime (1/2), the rest are multiples of 2. Over 6 you have half of two thirds of numbers that can possibly be prime, the rest are multiples of 2 or 3. Over 30 only 1/2 x 2/3 x 4/5 could possibly be prime. etc. This converges to the prime number theorem!

Anyway if anyone ever expresses amazement that's good to see but mathematically it's well known. Prime numbers have patterns in their frequency, specifically where there's period multiples of factors there can't possibly be primes. It's the basis for prime number theory and patterns in primes have been known since Erasthosenes was back in BC times. So if you see a pattern here just remember that the pattern comes from looking at a period of a composite number and within that composite number there's guaranteed periodic gaps in primes where the factors of that number repeat.

This btw is something mathematicians deal with a lot. People seem to think prime numbers have no patterns and any view of them that reveals patterns is a surprising which is a weird misconception. Prime numbers absolutely have patterns. It's the basis for prime number theory.

susam 10 hours ago [-]
A prime number greater than 3 must leave a remainder 1 or 5 when divided by 6. In other words:

If n is prime and n > 3, then n ≡ 1 (mod 6) or n ≡ 5 (mod 6).

Or more succinctly:

n ≡ ±1 (mod 6).

So when the total number of columns is a multiple of 6, all the primes greater than 3 line up on the nth columns for n = 1, 5, 7, 11, etc.

HocusLocus 8 hours ago [-]
There's a way to warp this in 3D and Contact two sides together to show the framework blueprint of a Portal device.
drkolip 7 hours ago [-]
The cake is a lie.
wordglyph 3 days ago [-]
So cool! I'd like to be able to start at any number
dduplex 3 days ago [-]
Thanks! Do you mean you’d like to be able to input any number you like?

The numbered values are editable inputs. You can click into them and type in the amount you’d like. If you couldn’t tell they were editable inputs, that’s valuable feedback! I’ll redesign them to make it more obvious that you can do so, and don’t have to just click the +/- buttons.

Cieric 12 hours ago [-]
I think they mean that the number that the image starts on is by default 0, I think they want to be able to change the starting number so top left could start at something else and increment from there. Adding on to that it would also be interesting to change what the number increments by.
wordglyph 3 hours ago [-]
No, I mean the starting prime number.
rg2004 8 hours ago [-]
Could be fun to turn these into initial conditions for Game of Life
eps 10 hours ago [-]
Ha, super, thanks for making it.

Check out 431 columns - this yields no obvious persistent patterns.

cgijoe 9 hours ago [-]
You sure about that? I see some clear diagonal lines at 431 cols: https://imgur.com/a/rNKjTtr
ljsprague 9 hours ago [-]
I think you'll find this to be the case with any prime.
smusamashah 10 hours ago [-]
Add option to skip all even numbers, another to skip all numbers ending with 5. Also, a way to see the number when you click a pixel or space.

It's fun seeing all these patterns. I did some edit. I was thinking that skipping over numbers divisible by 2 and 5 will get rid of most visible gaps, but they keep emerging.

hobo_in_library 11 hours ago [-]
The density of prime numbers remains remarkably consistent as you increase the grid size. Even the end of a 10,000 x 10,000 grid had just as many primes per inch as the earlier numbers