How to resolve the algorithm Goldbach's comet step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Goldbach's comet step by step in the jq programming language

Table of Contents

Problem Statement

Goldbach's comet is the name given to a plot of the function g(E), the so-called Goldbach function. The Goldbach function is studied in relation to Goldbach's conjecture. The function g(E) is defined for all even integers E>2 to be the number of different ways in which E can be expressed as the sum of two primes.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Goldbach's comet step by step in the jq programming language

Source code in the jq programming language

def count(s): reduce s as $_ (0; .+1);

def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;

def nwise($n):
  def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
  n;

def is_prime:
  . as $n
  | if ($n < 2)         then false
    elif ($n % 2 == 0)  then $n == 2
    elif ($n % 3 == 0)  then $n == 3
    elif ($n % 5 == 0)  then $n == 5
    elif ($n % 7 == 0)  then $n == 7
    elif ($n % 11 == 0) then $n == 11
    elif ($n % 13 == 0) then $n == 13
    elif ($n % 17 == 0) then $n == 17
    elif ($n % 19 == 0) then $n == 19
    else
      ($n | sqrt) as $rt
      | 23
      | until( . > $rt or ($n % . == 0); .+2)
      | . > $rt
    end;

# emit nothing if . is odd
def G:
  select(. % 2 == 0)
  | count( range(2; (./2)+1) as $i
           | select(($i|is_prime) and ((.-$i)|is_prime)) );

def task1:
  "The first 100 G numbers:",
  ([range(4; 203; 2) | G] | nwise(10) | map(lpad(4)) | join(" "));

def task($n):
  $n, 4, 22
  |"G(\(.)): \(G)";

task1, "", task(1000000)

  

You may also check:How to resolve the algorithm 15 puzzle solver step by step in the Raku programming language
You may also check:How to resolve the algorithm Array length step by step in the Maple programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Elixir programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Scheme programming language
You may also check:How to resolve the algorithm Miller–Rabin primality test step by step in the Seed7 programming language