top | item 44949162

Prime Number Grid

296 points| todsacerdoti | 6 months ago |susam.net

96 comments

order
[+] susam|6 months ago|reply
Hello! I wrote this simple prime number grid visualiser last night, just for fun. It is inspired by the "Show HN" post https://news.ycombinator.com/item?id=44888548 that I stumbled upon a few days ago.

My tool uses the Miller-Rabin primality test with prime bases drawn from https://oeis.org/A014233 to determine whether a number is prime. This allows it to handle numbers up to 3317044064679887385961980.

For example, https://susam.net/primegrid.html#3317044064679887385961781-2... shows the upper limit of the numbers this tool can check. The three circles displayed there represent the following prime numbers:

  3317044064679887385961783
  3317044064679887385961801
  3317044064679887385961813
I hope this is fun for you too!
[+] Tepix|6 months ago|reply
Great visualization! Can you please add a on-mouse-over so when i hover my mouse over a dot i can see the prime number it represents?

Would we see new patterns emerge if the number of columns increases per row by X (X being constant or perhaps prime numbers ;-) )?

[+] camillomiller|6 months ago|reply
It's so cool actually!

You actually sent me on a rabbit hole trying to visually look for patterns :D But I guess the discretionality with which you can organize in rows and columns makes mine quite a pointless excercise :D

[+] reb|6 months ago|reply
Thank you for making and sharing this! It's fun to quickly increment the column counter and spot repeating patterns over time — little spiral movements, big swinging lines.

Growing up I loved math's logic puzzle elements, but it got tough when presentation of the subject became more abstract in late high school and college. Visualization tools like this would have gone a long way toward making the concepts concrete and keeping me curious about the relationships behind the symbols.

[+] davedx|6 months ago|reply
I think another interesting feature would be if you could change the number base to 16 or some other base, I'm really curious if the pattern would change.
[+] mg|6 months ago|reply
Here is a strange one:

You look at integers in "packs" of 100. If a pack contains a prime number, you color it black, otherwise you color it red.

The first pack contains 100 consecutive integers. The second every second integer. The third every third integer and so on.

Every pack starts where the last one stopped.

On the first row, you draw 1 pack, on the second 2, on the third 3 and so on:

https://www.gibney.org/parallax_primes

It looks like hieroglyphs from another universe.

I'm still not sure why it looks the way it looks.

If you want to compare it to a random distribution, you can change this line:

    if (isPrime(myNum)) return 1;
To this:

    if (Math.random()>0.99) return 1;
Very different. I wonder where the symmetry and all the other properties of the pattern come from when using primes.
[+] davidnc|6 months ago|reply
This comment does a great job of clarifying the picture: https://news.ycombinator.com/item?id=17106193.

It's effectively a visualization of gcd(x,y), and has almost nothing to do with primes. Once you realize that, it's a lot easier to reason about a lot of the patterns, although it is still a pretty interesting visualization.

[+] Chinjut|6 months ago|reply
Your description here does not quite match your linked code, in that it is not that the N-th pack contains integers spaced out by N. Rather, packs on the N-th row contain integers spaced out by N. For example, the third pack does not contain "every third integer", but rather draws alternating integers just like the second pack, because it is on the second row. The second pack contains (first cell of the second row) contains {101, 103, 105, ..., 299} and the third pack (second cell of the second row) contains {102, 104, 106, ..., 300}.

With this in mind, the seeming patterns of the figure you link to are explained by https://news.ycombinator.com/item?id=17106193

[+] photonthug|6 months ago|reply
Ok this nerd-sniped me pretty good, never seen this before and assumed it would be quickly connected to the Ulam spiral mentioned elsewhere in the the thread. That particular rabbit hole kinda bottoms out in polynomial residues and the very mysterious-sounding "Conjecture F" [0].

This parallax primes thing though led to the linked page [1] which has lots of background and other connections, including the most satisfying part, which turned out more geometric [2]

[0] https://en.wikipedia.org/wiki/Ulam_spiral#Explanation [1] https://www.novaspivack.com/science/we-have-discovered-a-new... [2] https://www.cut-the-knot.org/Curriculum/Arithmetic/PrimesFro...

[+] xerox13ster|6 months ago|reply
I've been messing with this and you can get a very detailed view of another highly self-similar structure by changing the packsize to 1, the cellsize to 2, and then adding packSize++; to the end of the drawRow function.
[+] shaunxcode|6 months ago|reply
I have continued to research this - should be publishing a zine about it soon!
[+] pinoy420|6 months ago|reply
It looks the way it does because we like to see patterns even where there are none. E.g. you see a number 696969 and this seems more significant than 482649 for whatever reason
[+] willvarfar|6 months ago|reply
Perhaps explore plotting the Ulam spiral too? https://en.wikipedia.org/wiki/Ulam_spiral

Of course the pressing question is, if this is the start field for a Conway game-of-life, do any interesting patterns evolve?

It would be fun to brute-force a few starting grid sizes and seeing how long the game continues. Games that last more than a few steps can be set aside for human evaluation.

Because if it turns out that some particular smallish grid or spiral of primes causes something interesting to happen in game-of-life, then you can imagine HN melting down!?

[+] madcaptenor|6 months ago|reply
Seconding the Ulam spiral. My first thought was "why can't I see the diagonals?" because I expected it to be the Ulam spiral.
[+] BrainBacon|6 months ago|reply
Not exactly the same, but I made this Ulam spiral generator more than a decade ago.

https://embed.plnkr.co/mdZX6C/

It isn't just doing primes though, instead the size of the dot generated is dependent on the number of even divisors for the number at that position.

[+] throw310822|6 months ago|reply
Kind of surprising, my intuitive idea of primes is that they become rarer much faster, while there's really a ton of them.
[+] rmrfchik|6 months ago|reply
Nice patterns are reveals when cols is prime.
[+] ethan_smith|6 months ago|reply
This happens because when columns = p (prime), numbers in each column share the same remainder mod p, creating visible diagonal patterns as multiples of p are eliminated from primality.
[+] madcaptenor|6 months ago|reply
Not so much that cols is prime as that cols+1 or cols-1 has lots of factors - see for example 25 or 91 or 119. But it does seem like numbers adjacent to primes have a lot of factors.
[+] spongebobism|6 months ago|reply
When the col is seven, there are a lot of diagonals going from top right to bottom left. When col is five, from top left to bottom right. Are runs of consecutive sexy primes also this frequent for larger numbers, or does that pattern break down at some point?
[+] amne|6 months ago|reply
I find the patterns from cols % 30 == 0 very interesting (30,60,90,120, etc.) .. just straight vertical lines.

And if you go up or down by one (119 or 121) they appear to "rotate" left or right.

Very cool viz tool.

[+] scotty79|6 months ago|reply
Almost all of these patterns that you see don't really come from primes. If you display numbers not divisible by first 100 natural numbers you get pretty much the same picture.
[+] ilmenit|6 months ago|reply
I did recently also a tool for prime numbers visualization: https://ilmenit.github.io/prime-fold/ It's not only for visualization but also discovering of mathematical functions that generate or embed prime numbers using evolutionary algorithms and fitness functions. It has two modes: PrimeFold Mode (2D Embedding): Enter or evolve two functions, f_x(n) and f_y(n), to map numbers to 2D points. Primes and composites are visualized differently. This mode helps you discover spatial patterns and structures unique to primes. Example: f_x(n) = n, f_y(n) = n^2 or simply n, n^2. PrimeGen Mode (1D Generation): Enter or evolve a single function, f(n), to generate numbers. The app visualizes which outputs are prime and how many unique primes are produced. Example: f(n) = 2*n + 1.
[+] haar|6 months ago|reply
Setting it to 1, 7, 100 looks like a ticker tape of constellations (like Stargate Chevrons) :D
[+] anthk|6 months ago|reply
I'd love this but in SDL+GL allowing to scale up and down an image. Or better, a command to write an XBM/XPM image and then I'd convert it to any format I like.
[+] vintermann|6 months ago|reply
Fun to see that prime numbers of columns causes stripy patterns, and some stripe to the left and some stripe to the right. Probably some deep number theoretic reason for that.
[+] dfee|6 months ago|reply
My intuition was that there were even fewer primes and there was a greater rate of decrease as the numbers got bigger – but it looks like there are so many! Even at [1, 10,000, 10,000] it seems pretty dense near the bottom, though perhaps less dense.

Apparently the average gap between primes is `log(n)`: https://en.wikipedia.org/wiki/Prime_number_theorem

[+] ragmondo|6 months ago|reply
Here's a weird relationship between consecutive primes that I discovered while bored trying some python...

Take the last digit (in base 10) of consecutive primes. Now ignore 2 and 5 as .. well they only occur once, and look at the mapping between 1->3, 1->5, 1->7, 1->9 ... 3->1 etc etc..

You would think that they would pretty much all be equal, I mean primes are "random" right?

WRONG!

There are statistically significant differences in those edges, and no-one knows why.

[+] martinclayton|6 months ago|reply
Hours of fun (stimulation?) to be had...

Try these shapes: 100x113, then 100x114, then 100x115, the "patterns" swing from slant down, to vertical, to slant up.

I'd love this (even more) with some animation and colo(u)r options.

[+] themafia|6 months ago|reply
I'd like it if I could set "columns" to be an expression. That expression could be something like "1.1415 * row". That'd be neat.
[+] dirkc|6 months ago|reply
Nice visualization! 2 suggestions if I can nitpick :)

1. Make the grid render as a square when rows == columns

2. Default to the largest number of rows and columns that would still avoid page scrolling

[+] kitd|6 months ago|reply
Really interesting, stepping up and down the "cols" number, seeing the dots align at certain key points, especially at multiples of 30.
[+] mickeyp|6 months ago|reply
I really love susam's blog posts and curiosity. I highly recommend that people check out his site for more of his insights.
[+] susam|6 months ago|reply
Thank you for the kind words, Mickey! Your website too is a treasure trove of excellent information, especially for Emacs users like me!
[+] nyc111|6 months ago|reply
I tried but his pages do not have links to a home page or other posts
[+] cuber_messenger|6 months ago|reply
Decreasing the number of columns looks like rotating some noisy parallel lines counterclockwise. Very fun.