The arithmetic derivative
This example demonstrates the implementation and use of the arithmetic derivative algorithm in the Julia programming language.
Introduction
The arithmetic derivative is an analogue of the usual derivative function from mathematical analysis, but applied to integers. It was proposed to study the properties of numbers in terms of differentiation, similar to how derivatives are applied to functions. Formally, for a natural number , is denoted by , and is defined as follows:
- If – a prime number, then
- If Then )
It allows you to apply intuition from differential calculus to numerical sequences and is used in solving some problems in number theory.
This code implements a recursive version of this definition using prime factorization.
Implementation of the arithmetic derivative function D(n)
To use the function of checking for simplicity and factoring, you need the package 'Primes'
import Pkg; Pkg.add("Primes")
using Primes
function D(n)
# Если число отрицательное, вызываем D(-n) и меняем знак результата
n < 0 && return -D(-n)
# Для 0 и 1 значение D всегда равно 0
n < 2 && return zero(n)
# Если число простое, то его производная равна 1
isprime(n) && return one(n)
# Если число составное, используем формулу:
# D(n) = Σ (e_i * n / p_i), где p_i -- простые множители, e_i -- их степени в разложении
return typeof(n)(sum(e * n ÷ p for (p, e) in eachfactor(n)))
end
Function D(n) calculates the arithmetic derivative of a number n. Below is a detailed analysis of its implementation.:
- Processing of negative numbers is implemented through recursion with a sign change.
- The numbers 0 and 1 return 0, according to the definition.
- For prime numbers, 1 is returned.
- Decomposition is used for composite numbers
eachfactor(n)— we get pairs (prime, exponent). - For each prime divisor with a degree indicator let's summarize ; the result is converted to the type of the input argument.
Thus, the function completely satisfies the formal definition of an arithmetic derivative.
Calculating the arithmetic derivative
Output of values for the range from -99 to 100 in formatted format
foreach(p -> print(lpad(p[2], 5), p[1] % 10 == 0 ? "\n" : ""),
pairs(map(D, -99:100)))
Here we calculate the arithmetic derivatives of all integers in the range from -99 to 100, displaying them in a human-readable form.
Team map(D, -99:100) builds an array of values for each number in the interval.
Function pairs(...) creates index => value pairs so that the row number can be tracked.
foreach(...) passes through these pairs and outputs each value p[2], formatted via lpad in a 5-character field. If the pair number is divisible by 10 (p[1] % 10 == 0), there is a transition to a new line \n.
This gives us a table showing the behavior in a short interval.
Derivation of derived powers of ten
The next block calculates and prints the values for from 1 to 20:
for m in 1:20
result = D(Int128(10)^m) ÷ 7 # Вычисление D(10^m), затем деление на 7
println("D for 10^$(rpad(m, 3)) divided by 7 is $result")
end
Pay attention to:
- Conversion to
Int128: it is necessary because for large degrees of tens10^mit is possible to go beyond the standard typeInt. - Expression
D(Int128(10)^m)calculates the arithmetic derivative of a number10^m. - Integer division operation
÷it is needed to get an integer for a beautiful output.
This part of the code shows how the behavior of the derivative changes for powers of ten.
Conclusion
We have reviewed the implementation and operation of the arithmetic derivative algorithm in the Julia language.
The implementation includes the function D(n) corresponding to the formal definition for integers. A table of derivative values was created for a small range of numbers, and the behavior of the derivative for powers of ten was analyzed. This approach can help in studying the properties of arithmetic derivatives, and also shows Julia's power in quickly working with numerical problems from number theory. The example demonstrates a combination of precision in mathematical definitions and efficient work with large numbers through the use of specialized packages (in this case — Primes). The practice of readable presentation of results and handling of extreme cases was also shown.
The example was developed using materials from Rosetta Code