How to resolve the algorithm Strong and weak primes step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Strong and weak primes step by step in the jq programming language

Table of Contents

Problem Statement

Note that the definition for   strong primes   is different when used in the context of   cryptography.

Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Strong and weak primes step by step in the jq programming language

Source code in the jq programming language

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

# Emit {strong, weak} primes up to and including $n
def strong_weak_primes:
   . as $n
   | primes as $primes
   | ("\nCheck: last prime generated: \($primes[-1])" | debug) as $debug
   | reduce range(1; $primes|length-1) as $p ({};
       (($primes[$p-1] + $primes[$p+1]) / 2) as $x
       | if $primes[$p] > $x
         then .strong += [$primes[$p]]
         elif $primes[$p] < $x 
         then .weak += [$primes[$p]]
	 else .
	 end );

(1e7 + 19)
  | strong_weak_primes as {$strong, $weak}
  | "The first 36 strong primes are:",
    $strong[:36],
  "\nThe count of the strong primes below 1e6: \(count($strong[]|select(. < 1e6 )))",
  "\nThe count of the strong primes below 1e7: \(count($strong[]|select(. < 1e7 )))",

  "\nThe first 37 weak primes are:",
  $weak[:37],
  "\nThe count of the weak primes below 1e6: \(count($weak[]|select(. < 1e6 )))",
  "\nThe count of the weak primes below 1e7: \(count($weak[]|select(. < 1e7 )))"

  

You may also check:How to resolve the algorithm Bitmap/Read a PPM file step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Church numerals step by step in the Java programming language
You may also check:How to resolve the algorithm Compound data type step by step in the Vala programming language
You may also check:How to resolve the algorithm Benford's law step by step in the Ruby programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the PowerBASIC programming language