How to resolve the algorithm Unprimeable numbers step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Unprimeable numbers step by step in the jq programming language

Table of Contents

Problem Statement

As used here, all unprimeable numbers   (positive integers)   are always expressed in base ten.

───── Definition from OEIS ─────: Unprimeable numbers are composite numbers that always remain composite when a single decimal digit of the number is changed.

───── Definition from Wiktionary   (referenced from Adam Spencer's book) ─────: (arithmetic)   that cannot be turned into a prime number by changing just one of its digits to any other digit.   (sic)

Unprimeable numbers are also spelled:   unprimable. All one─ and two─digit numbers can be turned into primes by changing a single decimal digit.

190   isn't unprimeable,   because by changing the zero digit into a three yields   193,   which is a prime.

The number   200   is unprimeable,   since none of the numbers   201, 202, 203, ··· 209   are prime, and all the other numbers obtained by changing a single digit to produce   100, 300, 400, ··· 900,   or   210, 220, 230, ··· 290   which are all even.

It is valid to change   189   into   089   by changing the   1   (one)   into a   0   (zero),   which then the leading zero can be removed,   and then treated as if the   "new"   number is   89.

Show all output here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Unprimeable numbers step by step in the jq programming language

Source code in the jq programming language

def digits: tostring | explode | map([.] | implode | tonumber);

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

def variants:
  digits
  | range(0; length) as $pos
  | range(0;10) as $newdigit
  | if .[$pos] == $newdigit then empty
    else .[$pos] = $newdigit
    | join("")|tonumber
    end;
   
def is_unprimeable:
  if is_prime or any(variants; is_prime) then false
  else true
  end;

def unprimeables:
  range(4; infinite) | select(is_unprimeable);

def task:
  "First 35 unprimeables: ",
  [limit(35; range(0;infinite) | select(is_unprimeable))],

  "\nThe 600th unprimeable is \( nth(600 - 1; unprimeables) ).",

  "\nDigit  First unprimeable ending with that digit",
    "-----------------------------------------------",
  (range(0;10) as $dig
   | first( range(0;infinite) | select((. % 10 == $dig) and is_unprimeable))
   | "  \($dig)  \(lpad(9))" )
 ;

task

  

You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the REXX programming language
You may also check:How to resolve the algorithm Start from a main routine step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Arrays step by step in the Insitux programming language
You may also check:How to resolve the algorithm Loops/Nested step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the 11l programming language