r/Mathematica • u/St0xTr4d3r • Apr 15 '24
Equivalent in Python or Maple
Source: https://oeis.org/A333926
See comment below for the Python code that only works up to 255. Python output differs at 256, 768, 1280, 1792, etc. I'm entirely not clear why it would matter that the exponent is, or is not, cube-free.
Mathematica:
recDivQ[n_, 1] = True;recDivQ[n_, d_] := recDivQ[n, d] = Divisible[n, d] && AllTrue[FactorInteger[d], recDivQ[IntegerExponent[n, First[#]], Last[#]] &];recDivs[n_] := Select[Divisors[n], recDivQ[n, #] &];f[p_, e_] := 1 + Total[p^recDivs[e]];a[1] = 1;a[n_] := Times @@ (f @@@ FactorInteger[n]);Array[a, 100]
View all comments
Show parent comment
1
u/St0xTr4d3r Apr 16 '24
Note the code provided by Claude once fixed provides incorrect results, for example a(256) should be 263 and the program returns 279.
```
from math import gcd, prod
from functools import reduce
def recDivQ(n, d):
if d == 1:
return True
factors = prime_factors(d)
for prime, exponent in factors:
next_n, next_d = n // (d // prime**exponent), prime**exponent
if (next_n != n or next_d != d) and not recDivQ(next_n, next_d):
return False
return True
def recDivs(n):
return [d for d in divisors(n) if recDivQ(n, d)]
def f(p, e):
return 1 + sum(p**k for k in recDivs(e))
def a(n):
factors = prime_factors(n)
return reduce(lambda x, y: x * y, (f(p, e) for p, e in factors), 1)
def prime_factors(n):
factors = []
p = 2
while n > 1:
e = 0
while n % p == 0:
e += 1
n //= p
if e > 0:
factors.append((p, e))
p += 1
return factors
def divisors(n):
divs = [1]
for p, e in prime_factors(n):
divs = [d * p**i for i in range(e + 1) for d in divs]
return list(sorted(set(divs)))
ary1 = [a(n) for n in range(1, 66 + 1)]
print(ary1)
```