Skip to content

Instantly share code, notes, and snippets.

@hobwekiva
Created August 22, 2020 10:00
Show Gist options
  • Save hobwekiva/6683d950fd8dbb4169a1d3a4d720b585 to your computer and use it in GitHub Desktop.
Save hobwekiva/6683d950fd8dbb4169a1d3a4d720b585 to your computer and use it in GitHub Desktop.
@numba.jit("i4(i4, i4)", nopython=True, nogil=True)
def gcd(a, b):
while True:
if a == 0: return b
if b == 0: return a
if a == b: return a
if b > a:
a, b = a, b % a
else:
a, b = a % b, b
@numba.jit("f8(i4)", nopython=True, nogil=True)
def f(n):
arr = np.zeros(n, dtype=np.float64)
for i in np.arange(1, n + 1):
arr[i - 1] = np.log(gcd(i, n))
return np.exp(np.sum(arr) / n)
@numba.jit("i4[:](i4)", nopython=True, nogil=True)
def g(N):
result = np.zeros(N, dtype=np.int32)
n = 1
i = 0
fm = -np.inf
while i < N:
f0 = f(n)
if f0 > fm:
result[i] = n
i += 1
fm = f0
n += 1
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment