from numeric_base import D from decimal import ROUND_FLOOR,ROUND_CEILING,localcontext from math import isqrt from functools import lru_cache def decimal(x):return D(str(x)) @lru_cache(maxsize=1000) def density_lower(hi,beta='.09'): with localcontext() as c: c.prec=70 z=(decimal(hi)*decimal(beta)).exp() # Include every prime <= ceil(z), which only lowers the product. cap=int((z+max(D(1),z)*D('1e-50')).to_integral_value(rounding=ROUND_CEILING)) assert 12<=cap<=10**7 a=bytearray([1])*(cap+1);a[0]=a[1]=0 for p in range(2,isqrt(cap)+1): if a[p]:a[p*p::p]=bytes(len(a[p*p::p])) v=D(1);c.rounding=ROUND_FLOOR for p in range(3,cap+1,2): if a[p]:v*=D(p-2)/(p-1) return v