Arithmetic derivative

From Rosetta Code
Revision as of 23:25, 2 July 2022 by Wherrera (talk | contribs) (new task and python example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Arithmetic derivative is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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:

  • D(0) = D(1) = 0.
  • D(p) = 1 for any prime p.
  • D(mn) = D(m)n + mD(n) for any m,n ∈ N. (Leibniz rule for derivatives).

Additionally, for negative integers the arithmetic derivative may be defined as -D(-n) (n < 0).

Examples

D(2) = 1 and D(3) = 1 (both are prime) so if mn = 2 * 3, D(6) = (1)(3) + (1)(2) = 5.

D(27) = (1)(3) * 3 (three prime 3 factors) = 27.

D(30) = D(5)(6) + D(6)(5) = 6 + 5 * 5 = 31.

Task

Find and show the arithmetic derivatives for -99 through 100.

Stretch task

Find (the arithmetic derivative of 10^m) then divided by 7, where m is from 0 to 20.

See also


Python

from sympy.ntheory import factorint

def D(n):

   if n < 0:
       return -D(-n)
   elif n < 2:
       return 0
   else:
       fdict = factorint(n)
       if len(fdict) == 1: # is prime
           return 1
       return sum([n * e // p for p, e in fdict.items()])

for n in range(-99, 101):

   print('{:5}'.format(D(n)), end='\n' if n % 10 == 0 else )

print() for m in range(1, 21):

   print('D for 10**{} is {}'.format(m, D(10 ** m) // 7))

</lang>

Output:
  -75  -77   -1 -272  -24  -49  -34  -96  -20 -123
   -1 -140  -32  -45  -22 -124   -1  -43   -1 -176
   -1  -71  -18  -80  -55  -39   -1 -156   -1  -59
  -26  -72   -1  -61  -18   -1  -51  -33   -1  -92
   -1  -31  -22  -92  -16  -81   -1  -56  -20  -45
   -1 -112   -1  -25  -39  -48   -1  -41   -1  -68
  -16  -21   -1  -60  -12  -19  -14   -1   -1  -31
   -1  -32   -1  -15   -1  -44   -1  -13  -10  -24
   -1  -21   -1   -1   -8   -9   -1  -16   -1   -7
   -1   -1   -1   -5   -1   -1   -1   -1    0    0
    0    1    1    1    1    5    1    1    1    7
    1   16    1    9    8    1    1   21    1   24
   10   13    1   44    1   15    1   32    1   31
    1    1   14   19   12   60    1   21   16   68
    1   41    1   48   39   25    1  112    1   45
   20   56    1   81   16   92   22   31    1   92
    1   33   51    1   18   61    1   72   26   59
    1  156    1   39   55   80   18   71    1  176
    1   43    1  124   22   45   32  140    1  123
   20   96   34   49   24  272    1   77   75  140

D for 10**1 is 1
D for 10**2 is 20
D for 10**3 is 300
D for 10**4 is 4000
D for 10**5 is 50000
D for 10**6 is 600000
D for 10**7 is 7000000
D for 10**8 is 80000000
D for 10**9 is 900000000
D for 10**10 is 10000000000
D for 10**11 is 110000000000
D for 10**12 is 1200000000000
D for 10**13 is 13000000000000
D for 10**14 is 140000000000000
D for 10**15 is 1500000000000000
D for 10**16 is 16000000000000000
D for 10**17 is 170000000000000000
D for 10**18 is 1800000000000000000
D for 10**19 is 19000000000000000000
D for 10**20 is 200000000000000000000