Home > Blockchain >  What is wrong with this implementation of the arithmetic derivative?
What is wrong with this implementation of the arithmetic derivative?

Time:05-20

The following is a refactoring of the Python code found here for computing the arithmetic derivative.

from sympy import factorint

def f(n):
    result = factorint(n)
    result = result.items()
    result = [int(n*e/p) for p, e in result]
    result = sum(result)
    result = result if n > 1 else 0
    return result 

It can be shown that the arithmetic derivative of a primer number to the power of itself will be a fixed point. Feel free to scrutinize the proof if you think that is where the issue lies.

A051674 is the sequence of primes to the power of themselves in the usual ordering of the primes. Among them is 827240261886336764177, but when I call the above function on it I do not get the correct result:

>>> f(827240261886336764177)
827240261886336827392

In fact, tabulating all of the listed sequence members evaluated by f reveals that entries before that work, and entries after that break.

p_n f(p_n)
4 4
27 27
3125 3125
823543 823543
285311670611 285311670611
302875106592253 302875106592253
827240261886336764177 827240261886336827392
1978419655660313589123979 1978419655660313627328512
20880467999847912034355032910567 20880467999847910614749358850048
2567686153161211134561828214731016126483469 2567686153161211279596267358657515496669184

So it seems that the numbers becoming too large might be a problem. Why does this function fail on these larger cases?

CodePudding user response:

If you are dealing with integers, try to only use integer operations. Since in the line

result = [int(n*e/p) for p, e in result]

n, e and p are integers (int types), simply write n*e//p instead of int(n*e/p).

  • Related