How to resolve the algorithm Digital root/Multiplicative digital root step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Digital root/Multiplicative digital root step by step in the jq programming language

Table of Contents

Problem Statement

The multiplicative digital root (MDR) and multiplicative persistence (MP) of a number,

n

{\displaystyle n}

, is calculated rather like the Digital root except digits are multiplied instead of being added:

Show all output on this page. The Product of decimal digits of n page was redirected here, and had the following description The three existing entries for Phix, REXX, and Ring have been moved here, under ===Similar=== headings, feel free to match or ignore them.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Digital root/Multiplicative digital root step by step in the jq programming language

Source code in the jq programming language

def do_until(condition; next):
  def u: if condition then . else (next|u) end;
  u;

def mdroot(n):
  def multiply: reduce .[] as $i (1; .*$i);
  # state: [mdr, persist]
  [n, 0]
  | do_until( .[0] < 10;
              [(.[0] | tostring | explode | map(.-48) | multiply), .[1] + 1]
            );

# Produce a table with 10 rows (numbered from 0),
# showing the first n numbers having the row-number as the mdr
def tabulate(n):
  # state: [answer_matrix, next_i]
  def tab:
    def minlength: map(length) | min;
    .[0] as $matrix
    | .[1] as $i
    | if (.[0]|minlength) == n then .[0]
      else (mdroot($i) | .[0]) as $mdr
      | if $matrix[$mdr]|length < n then 
          ($matrix[$mdr] + [$i]) as $row
          | $matrix | setpath([$mdr]; $row)
        else $matrix
        end
      | [ ., $i + 1 ]
      | tab
      end;

  [[], 0]  | tab;

def neatly:
  . as $in
  | range(0;length) 
  | "\(.): \($in[.])";

def rjust(n): tostring | (n-length)*" " + .;

# The task:
"  i   : [MDR, MP]",
((123321, 7739, 893, 899998) as $i
 | "\($i|rjust(6)): \(mdroot($i))"),
"",
"Tabulation",
"MDR: [n0..n4]",
(tabulate(5) | neatly)

$ jq -n -r -c -f mdr.jq

  i   : [MDR, MP]
123321: [8,3]
  7739: [8,3]
   893: [2,3]
899998: [0,2]

Tabulation
MDR: [n0..n4]
0: [0,10,20,25,30]
1: [1,11,111,1111,11111]
2: [2,12,21,26,34]
3: [3,13,31,113,131]
4: [4,14,22,27,39]
5: [5,15,35,51,53]
6: [6,16,23,28,32]
7: [7,17,71,117,171]
8: [8,18,24,29,36]
9: [9,19,33,91,119]


  

You may also check:How to resolve the algorithm Arrays step by step in the Swift programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Nix programming language
You may also check:How to resolve the algorithm Function composition step by step in the J programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the Arturo programming language
You may also check:How to resolve the algorithm Penney's game step by step in the zkl programming language