Skip to content

Solution of Project Euler [484]

Published: at 03:42 PMSuggest Changes

Problem Archives can be found here.

Problem description

The arithmetic derivative is defined by

For example, 20=2420^{\prime}=24. Find gcd(k,k)\sum \operatorname{gcd}\left(k, k^{\prime}\right) for 1<k5×10151<k \leq 5 \times 10^{15}.

Note: gcd(x,y)\operatorname{gcd}(x, y) denotes the greatest common divisor of xx and yy.

Mathematical Derivation

Let g(n):=gcd(n,n)g(n):=\operatorname{gcd}\left(n^{\prime}, n\right) and f(pe):=g(pe)g(pe1)(f(n):f\left(p^e\right):=g\left(p^e\right)-g\left(p^{e-1}\right)(f(n): multiplicative function). Then,

k=2Ngcd(k,k)=1+n: powerful number Nnf(n).\sum_{k=2}^N \operatorname{gcd}\left(k, k^{\prime}\right)=-1+\sum_{n: \text { powerful number }}\left\lfloor\frac{N}{n}\right\rfloor f(n) .

Algorithm

import numba as nb
import numpy as np
from math import sqrt
from primesieve import primes

def euler484(L):
  pLst = primes(int(sqrt(L+0.5)))
  qLst = np.array(pLst)**2
  return L-1 + dfs(0,L,pLst,qLst)

@nb.njit
def dfs(i0,L0,pLst,qLst):
  res = 0
  for i in range(i0,len(pLst)):
    q = qLst[i]
    L = L0//q
    if L == 0:
      break
    p, e, g = pLst[i], 1, 1
    while L:
      gp = g
      e += 1
      if e != 1:
        if e == p:
          g *= q
          e = 0
        else:
          g *= p
        c = g - gp
        res += c*L
        if L > q:
          res += c*dfs(i+1,L,pLst,qLst)
      L //= p
  return res

N = 5 * 10 ** 15
print(euler484(N))

Previous Post
Deriving CQL-SAC
Next Post
Overview of Efficient Transformers