Plotting Successive Primes Modulo A Number     tl;dr: https://github.com/ology/Math/blob/master/primes/sequence-pairs

I have this passionate curiosity for seeing the primes “folded in on themselves” – constrained to a phase space. I also have a burning desire to visualize successive events. I suppose that I am searching for “strange attractors”, as with the famous water drip experiment.

I wanted to see the prime numbers this way, but they just ascend rapidly. Boring!

If they are “reduced” by dividing, modulo some number, they will be bounded (although still growing). Some examples will illustrate what I mean: 1 modulo 7 is 1. 2 modulo 7 is 2… 6 modulo 7 is 6. 7 modulo 7 is 0. If you do this to the prime number sequence (2, 3, 5, 7, 11, 13…), for 7 you get: 2, 3, 5, 0, 4, 6… For my visualization of successive primes, the points would therefore be (2,3), (3,5), (5,0), (0,4), (4,6)… – Never above the modulo, which is 7 in this case.

In order to generate the numbers and points, and visualize everything, I built the perl code above. An explanation follows:

I get the prime number sequence with the excellent Math::Prime::XS module. Then from user provided start and stop values, I iterate, taking each value as the modulo. Next, I get the new sequence of the modified primes (with the modulo):

Perl

for my \$mod ( \$start .. \$stop ) { my @sequence = map { \$_ % \$mod } @primes; ... formy\$mod(\$start..\$stop){[email protected]=map{\$_%\$mod}@primes;...

For the animation below, start was 2 and stop was 100. The number of primes below, my chosen number, 1000 is 97. So there are 96 points. Most overlap. This code creates x and y lists:

Perl

for my \$p ( 0 .. @sequence - 1 ) { if ( defined \$last ) { #print "[\$sequence[\$last],\$sequence[\$p]]/n"; push @x, \$sequence[\$last]; push @y, \$sequence[\$p]; \$last = \$p; } else { \$last = \$p; next; }} formy\$p([email protected]){if(defined\$last){#print "[\$sequence[\$last],\$sequence[\$p]]/n";[email protected],\$sequence[\$last];[email protected],\$sequence[\$p];\$last=\$p;}else{\$last=\$p;next;}}

Next I scatter plot the points by interfacing with the venerable, R:

Perl

\$R->run( "png(file='\$file')" );\$R->run( 'x <- c(' . join( ',', @x ) . ')' );\$R->run( 'y <- c(' . join( ',', @y ) . ')' );\$R->run( 'z <- c(' . join( ',', map { \$prop{\$_} } @y ) . ')' );\$R->run( "plot(x, y, pch=21, bg= gray(z), xlab='P', ylab='P+1', main='Successive Primes to \$limit Modulo \$mod')" );\$R->run( 'dev.off()' ); \$R->run("png(file='\$file')");\$R->run('x <- c('.join(',',@x).')');\$R->run('y <- c('.join(',',@y).')');\$R->run('z <- c('.join(',',map{\$prop{\$_}}@y).')');\$R->run("plot(x, y, pch=21, bg= gray(z), xlab='P', ylab='P+1', main='Successive Primes to \$limit Modulo \$mod')");\$R->run('dev.off()');

What is z, is an exercise left to the reader…

This runs for each sequence of “modulo primes.” For the animation, I chose to show 100 frames. Top