How to resolve the algorithm Prime conspiracy step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Prime conspiracy step by step in the Picat programming language

Table of Contents

Problem Statement

A recent discovery, quoted from   Quantamagazine   (March 13, 2016): and

The task is to check this assertion, modulo 10. Lets call    i -> j   a transition if    i   is the last decimal digit of a prime, and    j   the last decimal digit of the following prime.

Considering the first one million primes.   Count, for any pair of successive primes, the number of transitions    i -> j   and print them along with their relative frequency, sorted by    i . You can see that, for a given    i ,   frequencies are not evenly distributed.

(Modulo 10),   primes whose last digit is   9   "prefer"   the digit   1   to the digit   9,   as its following prime.

Do the same for one hundred million primes.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Prime conspiracy step by step in the Picat programming language

Source code in the picat programming language

go =>
  N = 15_485_863, % 1_000_000 primes
  Primes = {P mod 10 : P in primes(N)},
  Len = Primes.len,
  A = new_array(10,10), bind_vars(A,0),
  foreach(I in 2..Len)
    P1 = 1 + Primes[I-1], % adjust for 1-based
    P2 = 1 + Primes[I],
    A[P1,P2] := A[P1,P2] + 1
  end,
  foreach(I in 0..9, J in 0..9, V = A[I+1,J+1], V > 0)
    printf("%d -> %d count: %5d frequency: %0.4f%%\n", I,J,V,100*V/Len)
  end,
  nl.

  

You may also check:How to resolve the algorithm Special characters step by step in the Arturo programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Python programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the TI-83 BASIC programming language
You may also check:How to resolve the algorithm Random numbers step by step in the Kotlin programming language
You may also check:How to resolve the algorithm File size distribution step by step in the Action! programming language