6 ms·
Show HN: Prime Number Grid Visualizer
Hello HN. I made this simple little tool that let's you input rows and columns to create a grid, then it plots the grid with prime numbers.
I made it for fun, but I'd love suggestions on how I can improve it in any way. Thanks, love you.
- wordglyph 1y agoSo cool! I'd like to be able to start at any number
- dduplex 1y agoThanks! 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 1y agoI 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 1y agoNo, I mean the starting prime number.
- Towaway69 1y agoThe first thing I thought of was why not add the QR markers (i.e. the three squares) and then see if a prime grid is scannable. Perhaps a prime grid will generate a valid QR code that is itself a prime number in QR!
- navane 1y agoI'm probably an idiot but having the columns at a multiple of 6 is very pleasing
- AnotherGoodName 1y agoThe 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 1y agoA 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.
- kawfey 1y agoI 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-doublespeed.mov https://n0ssc.com/wp-content/uploads/2025/08/prime-doublespe...
- lurn_mor 1y agoVery! I saw the distant galaxies spin.
- jasonjmcghee 1y agoI did the same thing - but you can just put your cursor in the number box and hold up / down arrow key.
- artemonster 1y agoI did the same thing and saw galaxies
- physix 1y agoI was waiting for ..... ...... ...... .... .. .. ...... .. .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. .. ..... ...... ...... .. .. .. .. .. ... ..... ... .. .. .. .. ... .. .. .. .. .. .. .. .. .. ... .. ...... .. .. .. .... .. .. ..... to show up, until I realized I've been coding too much today.
- clueless 1y agowoah, yeah, what is up with the galaxies spin motion/transformation? there's gotta be a good explanation
- AnotherGoodName 1y agoStart 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.
- hobo_in_library 1y agoThe 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
- brandonasuncion 1y agoReminds 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-array-exactly-once-in-random-order/ https://lemire.me/blog/2017/09/18/visiting-all-values-in-an-...
- teiferer 1y agoThat'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.
- brandonasuncion 1y agoI use "random" in quotation marks because it communicates the topic effectively and is very straight to the point. Colloquially, when people say "random" in the context of algorithms, it's generally understood to mean "pseudorandom" or "quasirandom". The trick is essentially an LCG using coprime numbers. The blog post uses "random" in quotations as well in its title. I call it O(1) because the ordering is fully determined just by choosing an arbitrary prime number. It's constant time to "reorder" the elements and also constant time to get the i'th element in the permutation. This is in contrast to something like the the Fisher-Yates shuffle which permutes the elements in O(n) time.
- antirez 1y agoFeistel networks are another way to map your index from linear sequence to a pseudorandom permutation.
- brandonasuncion 1y agoI was reading into it and came across your blog post. It's a good followup read. It has better avalanching compared to indexing by `(i * prime) % n`, but at the tradeoff of `n` being restricted to powers of 2.
- eps 1y agoHa, super, thanks for making it. Check out 431 columns - this yields no obvious persistent patterns.
- smusamashah 1y agoAdd 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.
- mfoc 1y agoWhen 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?
- voisin 1y agoI am interested to know how you discovered this! Was it by happenstance or did you know to look for this?
- mfoc 1y agoI had noticed it before. The response by AnotherGoodName makes it clear to me now why.
- AnotherGoodName 1y agoThose 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 1y agoThat 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.
- deleted 1y ago[deleted]
- HocusLocus 1y agoThere's a way to warp this in 3D and Contact two sides together to show the framework blueprint of a Portal device.
- drkolip 1y agoThe cake is a lie.
- yummypaint 1y agoThis is very cool, it reminds me of an ulam spiral. https://en.m.wikipedia.org/wiki/Ulam_spiral https://en.m.wikipedia.org/wiki/Ulam_spiral
- 8bitsrule 1y agoIt'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!
- vismit2000 1y agoReminds of https://www.3blue1brown.com/lessons/prime-spirals https://www.3blue1brown.com/lessons/prime-spirals
- BrandoElFollito 1y agoI was about to post that too. The 3b1b post really goes into the reasons why this happens (as usual) butvalso shows there is no m1gic in math (which I am not sure is a good thing or not)
- rg2004 1y agoCould be fun to turn these into initial conditions for Game of Life
- throwmeaway222 1y agoSomeone's going to find the correct column count to reveal the hidden message to build an intergalactic space ship with FTL travel.
- heywire 1y agoReminds 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.
- yen223 1y agoA feature Id like to see is an option to invert the grid, i.e. show the composites instead of the primes
- noduerme 1y agoThe trick is to do 88 columns, punch them out on a roll, and run it through a player piano.
- dmje 1y agothis deserves more upvotes
- gus_massa 1y agoIf someones implements it in a webpage, I expect it to be a nice post for HN. It will sound horrible. IIRC if you know music and choose only 4 notes correctly, even random notes may not sound too bad. So perhaps the version with 88/12*4 columns may be better.
- dont_kill3122 1y agokick miamia9423 in Roblox
- kentbrew 1y agocolumns = 420 has an interesting set of troughs down the sides and middle.
- shankarpriyank 1y agocool