Extra on Prime Parallelograms

Extra on Prime Parallelograms

Introduction

I used to be
studying an article about tips on how to graph the “prime parallelograms” in J utilizing verbs, which relies on the second a part of video by Numberphile.

The
concept is to take the
nth prime and subtract the quantity that’s obtained by
inverting the order of its illustration in base 2. For instance, the 16th
prime is 53 = 1101012, once we reverse it we get 1010112
= 43, so the worth is 53 – 43 = 10. If we graph this operate, parallelograms
seem.

I
puzzled what would occur if we graph all of the numbers as a substitute of solely the
primes. Additionally, I wished to grasp the place these parallelograms come from.

Further
modules

To
graph them in Racket we might want to use some further modules, so we begin
with

#lang
racket


(require
math/number-theory)


(require
plot)


#;(plot-new-window?
#t)

I additionally
like to vary how the numbers seem on the axes. It’s a technical half, and it
virtually doesn’t change the outcome. The code is on the backside.

Rebuilding
the graph

First,
we outline the operate that reverses the order of the bits within the binary
illustration of the quantity.

(outline
(binary-reverse x)


 
(let loop ([x x] [r 0])


   
(if (zero? x)


      
r


   
   (loop (quotient x 2) (+ (* 2 r) (the rest x 2))))))

Let’s
see some examples

(for
([i (in-range 16)])


 
(outline r (binary-reverse i))


 
(displayln (checklist i r (number->string i 2) (number->string r 2))))

>
(zero zero zero zero)


>
(1 1 1 1)


>
(2 1 10 1)


>
(three three 11 11)


>
(four 1 100 1)


>
(5 5 101 101)


>
(6 three 110 11)


>
(7 7 111 111)


>
(eight 1 1000 1)


>
(9 9 1001 1001)


>
(10 5 1010 101)


>
(11 13 1011 1101)


>
(12 three 1100 11)


>
(13 11 1101 1011)


> (14 7 1110 111)

>
(15 15 1111 1111)

Observe:
It’s doable to outline this operate utilizing strings, however utilizing strings is
at all times a lot slower than working instantly with integers.

To
simplify the notation, let’s name
inv this
operate that reverses the order of the bits,
p the
operate that calculates the
nth prime
and let’s name
f the operate f(n)
= n – inv(n)
.

Now we
could make an inventory with the factors
(n, f(p(n))) = (n, p(n) –
inv(p(n)))
as vectors.

(outline
prim/authentic (for/checklist ([i (in-naturals 1)]


                                
[p (in-list (next-primes 1 10000))])


                       
(vector i (- p (binary-reverse p)))))

Observe:
We may write our personal features to check primality, however the build-in
next-primes and prime? are higher.

And we
draw it with
plot

(plot (factors prim/authentic))

We repair
the colour and opacity to make the drawing extra good

(plot #:title “Prime Paralelograms
(authentic)”


     
(factors prim/authentic


             
#:sym ‘dot


             
#:colour ‘blue #:alpha .5)


     
#:x-label “n” #:y-label “f(p(n))”

      #:x-min zero)

To
save the picture we use
plot-file

(plot-file #:title “Prime Paralelograms
(authentic)”


      
    (factors prim/authentic


                  
#:sym ‘dot


                  
#:colour ‘blue #:alpha .5)


          
“prime-paralelograms(authentic).png”


          
#:x-label “n” #:y-label “f(p(n))”

           #:x-min zero)

In
order to check it with the subsequent graph, we draw it once more. However with the labels
of the values of the x-axis aligned to the left (in order that the sting
of the graph is the sting of the picture).

(parameterize ([plot-x-tick-label-anchor ‘top-right])

 
(plot #:title “Prime Paralelograms (authentic)”


       
(factors prim/authentic


               
#:sym ‘dot


               
#:colour ‘blue #:alpha .5)


       
#:x-label “n” #:y-label “f(p(n))”

        #:x-min zero))

Now we
change what we graph. We alter the x a part of the factors, as a substitute of utilizing in
which place of the checklist of prime it’s, we use the worth of the prime, that
is, as a substitute of drawing
(n, f(p(n)) we draw (p(n),
f(p(n)) = (p, f(p))
.

(outline prim/new (for/checklist ([i (in-naturals 1)]

                     
      [p (in-list (next-primes 1 10000))])


                  
(vector p (- p (binary-reverse p)))))

(parameterize
([plot-x-tick-label-anchor ‘top-right])


 
(plot #:title “Prime Paralelograms”


       
(factors prim/new


               
#:sym ‘dot


          
     #:colour ‘blue #:alpha .5)


       
#:x-label “p(n)” #:y-label “f(p(n))”


       
#:x-min zero))

We will examine it with the earlier one:

We get
a really comparable drawing if we ignore the dimensions of the x-axis. The parallelograms
are lower at barely totally different positions, however they’re very comparable.

What
occurs is that if we draw the n
th prime we get virtually a line.

(outline prim/scale (for/checklist ([i (in-naturals 1)]

                             
[p (in-list (next-primes 1 10000))])


          
          (vector i p)))

(plot
#:title “Primes”


     
(factors prim/scale


             
#:sym ‘dot)


     
#:x-label “n” #:y-label “p(n) = nth prime”


     
#:x-min zero)

The
inverse is the operate that counts the variety of primes that in
common known as
pi(n) and its slope is slowly
altering, roughly like
1/log(n), however log(n) is a
operate that grows very slowly. So the change between the graphs is sort of a
linear transformation. Subsequently, once we use it to vary the x-axis within the
earlier drawings we see that the form of the parallelograms modifications little or no.
(We must always have the ability to discover extra distortion within the first parallelograms.)

Evaluating
with all numbers

Now
let’s have a look at how the graph seems once we use all of the numbers as a substitute of simply the
primes. To have the ability to examine the graph higher, we repair the identical vary for the y-axis
in all of the graphs. Additionally, I like to decide on a chart measurement in order that the road y=x has
roughly 45°. (In Excel you make a graph after which with the mouse you repair
all the main points of the axes and scales. Right here you must put all that fixes in
this system in order that the scales are precisely as you need.)

(outline all (for/checklist ([i (in-range 1 131072)])

             
(vector i (- i (binary-reverse i)))))

(plot
#:title “All Paralelograms”


     
(factors all


             
#:sym ‘dot


             
#:colour ‘black #:alpha .1)


     
#:x-label “n” #:y-label “f(n)”

      #:y-min -65536 #:y-max 131072

     
#:x-min zero


     
#:width 400 #:top 600)

Let’s repeat
one of many earlier graph, however on these new scales. We are going to create the checklist of
primes in a barely totally different means.

(outline prim (for/checklist ([i (in-range 1 131072)]

                       
#:when (prime? i))


             
(vector i (- i (binary-reverse i)))))

(plot
#:title “Prime Paralelograms”


     
(factors prim


             
#:sym ‘dot


             
#:colour ‘blue #:alpha .5)


     
#:x-label “p” #:y-label “f(p)”


     
#:y-min -65536 #:y-max 131072


     
#:x-min zero


     
#:width 400 #:top 600)

And we
superimpose the final two graphs

(plot #:title “All vs Prime Paralelograms”

     
(checklist (factors all


                   
#:sym ‘dot


                   
#:colour ‘black #:alpha .1)


           
(factors prim


                   
#:sym ‘dot


                   
#:colour ‘blue #:alpha .5))


     
#:x-label “n” #:y-label “f(n)”

      #:y-min -65536 #:y-max 131072

     
#:x-min zero


     
#:width 400 #:top 600)

What
we see is that the prime parallelograms occupy the underside half of
the parallelograms which are fashioned once we graph all of the numbers.

Even
and odd

Virtually
all primes are odd. As they’re odd, while you invert the order of
the bits in binary you get a quantity with the identical variety of figures in binary.
So the outcome has about the identical measurement. You may certain the outcome and formalize
this concept, however it’s nicer to check in a graphic what occurs once we change
prime numbers with odd numbers.

(outline odd (for/checklist ([i (in-range 1 131072 2)])

             
(vector i (- i (binary-reverse i)))))

(plot
#:title “All vs Odd Paralelograms”


     
(checklist (factors all


                   
#:sym ‘dot


                   
#:colour ‘black #:alpha .1)


           
(factors odd


                   
#:sym ‘dot


                   
#:colour ‘pink #:alpha .1))


     
#:x-label “n” #:y-label “f(n)”

      #:y-min -65536 #:y-max 131072

     
#:x-min zero


     
#:width 400 #:top 600)

We see
that the odd quantity occupy primarily the identical parallelograms, though there
are fewer gaps.

On the
different hand, the even numbers finish in zero in binary, so by reversing the order of
their bits in binary they lose not less than one determine, so the result’s smaller.
After we draw them we get this graph.

(outline even (for/checklist ([i (in-range 2 131072 2)])

              
(vector i (- i (binary-reverse i)))))

(plot
#:title “All vs Even Paralelograms”


     
(checklist (factors all


                   
#:sym ‘dot


                   
#:colour ‘black #:alpha .1)


           
(factors even


                   
#:sym ‘dot


                   
#:colour ‘inexperienced #:alpha .1))


     
#:x-label “n” #:y-label “f(n)”

      #:y-min -65536 #:y-max 131072

     
#:x-min zero


     
#:width 400 #:top 600)

We see
that the even numbers primarily occupy the higher halves, that’s, they don’t
overlap with what’s painted by the primes. So the holes within the prime parallelograms
are on account of non-prime odd numbers.

Some concepts
to attempt

  • What
    occurs in base three? Will we get the plain generalization? In base four, will we get
    flags?
  • Redraw
    to see solely the primary parallelograms. The change of scale will not be so linear at
    the start, so when drawing the parallelograms of the unique article
    (n, f(p(n)) they need to be extra distorted.
  • Redraw
    all the pieces with the x-axis in logarithmic scale, so that every one parallelograms
    have the identical width and you may see the primary ones simpler. I do not know what
    scale to make use of for the y-axis. Perhaps you must draw every parallelogram
    individually?
  • The
    prime numbers aren’t random, however they appear fairly random. It
    is perhaps attention-grabbing to generate an inventory of fake-primes, filtering the odd
    numbers with a distribution much like that of the primes and see
    how the graph seems.

Altering
the format of the labels

I like
to vary how the numbers seem on the axes, I do not like that
100000
seems as
105. This half is technical and
virtually doesn’t change the outcome, so I cannot clarify the main points. (Appears
like a good suggestion for a PR as an extra choice for
linear-ticks.)

(require plot/utils)

(outline
((linear-ticks-format/no-sc) min max pre-ticks)


 
(outline digits (digits-for-range min max))


 
(map (lambda (pt)


        
(real->plot-label (pre-tick-value pt) digits #f))


      
pre-ticks))


(outline
(linear-ticks/no-sc) (ticks (linear-ticks-layout) (linear-ticks-format/no-sc)))


(plot-x-ticks
(linear-ticks/no-sc)) ; virtually world change


(plot-y-ticks
(linear-ticks/no-sc)) ; virtually world change

Full
code

#lang racket

(require
math/number-theory)


(require
plot)


#;(plot-new-window?
#t)

(require
plot/utils)


(outline
((linear-ticks-format/no-sc) min max pre-ticks)


 
(outline digits (digits-for-range min max))


 
(map (lambda (pt)


        
(real->plot-label (pre-tick-value pt) digits #f))


      
pre-ticks))


(outline
(linear-ticks/no-sc) (ticks (linear-ticks-layout) (linear-ticks-format/no-sc)))


(plot-x-ticks
(linear-ticks/no-sc)) ; virtually world change


(plot-y-ticks
(linear-ticks/no-sc)) ; virtually world change

(outline
(binary-reverse x)


 
(let loop ([x x] [r 0])


   
(if (zero? x)


      
r


      
(loop (quotient x 2) (+ (* 2 r) (the rest x 2))))))

(for
([i (in-range 16)])


 
(outline r (binary-reverse i))


 
(displayln (checklist i r (number->string i 2) (number->string r 2))))

#;(for
([i (in-range 128)]


       
#:when (prime? i))


   
(outline r (binary-reverse i))


   
(displayln (checklist i r (number->string i 2) (number->string r 2))))

;ORIGINAL

(outline
prim/authentic (for/checklist ([i (in-naturals 1)]


                                
[p (in-list (next-primes 1 10000))])


                       
(vector i (- p (binary-reverse p)))))

(plot
(factors prim/authentic))

(plot
#:title “Prime Paralelograms (authentic)”


     
(factors prim/authentic


             
#:sym ‘dot


             
#:colour ‘blue #:alpha .5)


     
#:x-label “n” #:y-label “f(p(n))”

      #:x-min zero)

(plot-file
#:title “Prime Paralelograms (authentic)”


          
(factors prim/authentic


                  
#:sym ‘dot


                  
#:colour ‘blue #:alpha .5)


          
“prime-paralelograms(authentic).png”


          
#:x-label “n” #:y-label “f(p(n))”

           #:x-min zero)

(parameterize
([plot-x-tick-label-anchor ‘top-right])


 
(plot #:title “Prime Paralelograms (authentic)”


       
(factors prim/authentic


               
#:sym ‘dot


               
#:colour ‘blue #:alpha .5)


       
#:x-label “n” #:y-label “f(p(n))”

        #:x-min zero))

;NEW

(outline
prim/new (for/checklist ([i (in-naturals 1)]


                           
[p (in-list (next-primes 1 10000))])


                  
(vector p (- p (binary-reverse p)))))

(parameterize
([plot-x-tick-label-anchor ‘top-right])


 
(plot #:title “Prime Paralelograms”


       
(factors prim/new


               
#:sym ‘dot


               
#:colour ‘blue #:alpha .5)


       
#:x-label “p(n)” #:y-label “f(p(n))”


       
#:x-min zero))

;SCALE

(outline
prim/scale (for/checklist ([i (in-naturals 1)]


                             
[p (in-list (next-primes 1 10000))])


                    
(vector i p)))

(plot
#:title “Primes”


     
(factors prim/scale


             
#:sym ‘dot)


     
#:x-label “n” #:y-label “p(n) = nth prime”


     
#:x-min zero)

;All

(outline
all (for/checklist ([i (in-range 1 131072)])


             
(vector i (- i (binary-reverse i)))))

(plot
#:title “All Paralelograms”


     
(factors all


             
#:sym ‘dot


             
#:colour ‘black #:alpha .1)


     
#:x-label “n” #:y-label “f(n)”

      #:y-min -65536 #:y-max 131072

     
#:x-min zero


     
#:width 400 #:top 600)

(outline
prim (for/checklist ([i (in-range 1 131072)]


                       
#:when (prime? i))


             
(vector i (- i (binary-reverse i)))))

(plot
#:title “Prime Paralelograms”


     
(factors prim


             
#:sym ‘dot


             
#:colour ‘blue #:alpha .5)


     
#:x-label “p” #:y-label “f(p)”


     
#:y-min -65536 #:y-max 131072


     
#:x-min zero


     
#:width 400 #:top 600)

(plot
#:title “All vs Prime Paralelograms”


     
(checklist (factors all


                   
#:sym ‘dot


                   
#:colour ‘black #:alpha .1)


           
(factors prim


                   
#:sym ‘dot


                   
#:colour ‘blue #:alpha .5))


     
#:x-label “n” #:y-label “f(n)”

      #:y-min -65536 #:y-max 131072

     
#:x-min zero


     
#:width 400 #:top 600)

;
ODD and EVEN


(outline
odd (for/checklist ([i (in-range 1 131072 2)])


             
(vector i (- i (binary-reverse i)))))

(plot
#:title “All vs Odd Paralelograms”


     
(checklist (factors all


                   
#:sym ‘dot


                   
#:colour ‘black #:alpha .1)


           
(factors odd


                   
#:sym ‘dot


                   
#:colour ‘pink #:alpha .1))


     
#:x-label “n” #:y-label “f(n)”

      #:y-min -65536 #:y-max 131072

     
#:x-min zero


     
#:width 400 #:top 600)

(outline
even (for/checklist ([i (in-range 2 131072 2)])


              
(vector i (- i (binary-reverse i)))))

(plot
#:title “All vs Even Paralelograms”


      (checklist
(factors all


                   
#:sym ‘dot


                   
#:colour ‘black #:alpha .1)


           
(factors even


                   
#:sym ‘dot


                   
#:colour ‘inexperienced #:alpha .1))


     
#:x-label “n” #:y-label “f(n)”

      #:y-min -65536 #:y-max 131072

     
#:x-min zero


     
#:width 400 #:top 600)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.