How to resolve the algorithm Arithmetic derivative step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Arithmetic derivative step by step in the jq programming language

Table of Contents

Problem Statement

The arithmetic derivative of an integer (more specifically, the Lagarias arithmetic derivative) is a function defined for integers, based on prime factorization, by analogy with the product rule for the derivative of a function that is used in mathematical analysis. Accordingly, for natural numbers n, the arithmetic derivative D(n) is defined as follows: Additionally, for negative integers the arithmetic derivative may be defined as -D(-n) (n < 0). D(2) = 1 and D(3) = 1 (both are prime) so if mn = 2 * 3, D(6) = (1)(3) + (1)(2) = 5. D(9) = D(3)(3) + D(3)(3) = 6 D(27) = D(3)*9 + D(9)*3 = 9 + 18 = 27 D(30) = D(5)(6) + D(6)(5) = 6 + 5 * 5 = 31. Find and show the arithmetic derivatives for -99 through 100. Find (the arithmetic derivative of 10^m) then divided by 7, where m is from 1 to 20.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic derivative step by step in the jq programming language

Source code in the jq programming language

To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);

# In case gojq is used:
def _nwise($n):
  def nw: if length <= $n then . else .[0:$n] , (.[$n:] | nw) end;
  nw;

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

def D($n):
    if   $n < 0 then -D(- $n)
    elif $n < 2 then 0
    else [$n | factors] as $f
    | ($f|length) as $c
    | if   $c <= 1 then 1
      elif $c == 2 then $f[0] + $f[1]
      else ($n / $f[0]) as $d
      | D($d) * $f[0] + $d
      end
    end ;

def task:
  def task1:
    reduce range(-99; 101) as $n ([]; .[$n+99] = D($n))
    | _nwise(10) | map(lpad(4)) | join(" ");

  def task2:
    range(1; 21) as $i
    | "D(10^\($i)) / 7 = \( D(10|power($i))/7 )" ;

  task1, "", task2 ;

task

  

You may also check:How to resolve the algorithm Morse code step by step in the Forth programming language
You may also check:How to resolve the algorithm Scope/Function names and labels step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Multifactorial step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Compile-time calculation step by step in the Wren programming language
You may also check:How to resolve the algorithm Host introspection step by step in the C# programming language