Skip to content

Instantly share code, notes, and snippets.

@brezhart
Created March 9, 2022 17:39
Show Gist options
  • Save brezhart/a2ca9571494429805b4132445b585350 to your computer and use it in GitHub Desktop.
Save brezhart/a2ca9571494429805b4132445b585350 to your computer and use it in GitHub Desktop.
calculating coefficients
LEFT_BOUND = 1
RIGHT_BOUND = 127
NEED_FACTORIAL_IN_DENUMINATOR = True
import math
import numpy
from fractions import *
def fact(n):
p = 1
for i in range(1,n+1):
p*=i
return p
def get(n,c):
dp = [[1]*(c+1)]
dp[0][0] = 0
for i in range(1,n):
dp.append([0]*(c+1))
for g in range(1,c+1):
for j in range(g, c+1, g):
dp[i][g] += dp[i-1][j]
s = 0
for i in range(c+1):
s+=dp[n-1][i]
return s
def getCoefficients(c):
l = int(1e10)
logC = math.floor(math.log2(c))
ys = [get(i, c) for i in range(1, logC+2)]
xs = list(range(1, logC+2))
coefs = list(numpy.polyfit(xs, ys, logC))
for i in range(len(coefs)):
coefs[i] = math.floor(coefs[i]*l)/l
coefs[i] = Fraction(coefs[i]).limit_denominator()
if (NEED_FACTORIAL_IN_DENUMINATOR):
for i in range(len(coefs)):
mult = fact(logC+i)//(coefs[i].denominator)
coefs[i] = (( coefs[i].numerator*mult, coefs[i].denominator*mult) )
return coefs
res = []
for i in range(LEFT_BOUND, RIGHT_BOUND):
res.append(getCoefficients(i))
for i in range(len(res)):
st = ""
for g in range(len(res[i])):
st+= f"{res[i][g][0]}/{res[i][g][1]} "
print(LEFT_BOUND+i, ": ", st)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment