"""Upper discrepancy for a moving prime interval using one modest exact prime prefix.""" from functools import lru_cache from decimal import ROUND_FLOOR,ROUND_CEILING from numeric_base import D,PR,sum1,sum2,PFX,CUT,LOGCUT,guard,upfloat,dnfloat,expv,np @lru_cache(maxsize=2) def prefix(k): ds=sum1 if k==1 else sum2 ll=np.array([upfloat(D(int(p)).ln().ln()) for p in PR]) ll0=np.array([dnfloat(D(int(p)).ln().ln()) for p in PR]) lows=np.nextafter(np.nextafter((ds[:-1]-len(PR)).astype(float)/(PFX),-np.inf)-ll,-np.inf) highs=np.nextafter(np.nextafter(ds[1:].astype(float)/PFX,np.inf)-ll0,np.inf) # Nontrivial infinitesimal endpoint conventions in the prime range are both enclosed. c=D(int(ds[-1]))/PFX-LOGCUT.ln();tail=(2*expv(-LOGCUT/2)+D('.0000098'))/LOGCUT+expv(-LOGCUT)+D(k)/(CUT-k-1) return lows,highs,c+tail+D('1e-35'),c-tail-D(len(PR))/PFX-D('1e-35') def around(t,down): x=expv(t);return int((x+(-guard(x) if down else guard(x))).to_integral_value(rounding=ROUND_FLOOR if down else ROUND_CEILING)) @lru_cache(maxsize=40000) def discrepancy(a,b,k=1,high_beyond=True): # The lower endpoint moves between exp(a) and exp(b). ge=(2*expv(-a/2)+D('.0000098'))/a+expv(-a)+D(k)/(expv(a)-k-1) if a>=LOGCUT:return ge+D('1e-35') lo,high,cmax,cmin=prefix(k);ds=sum1 if k==1 else sum2 first=int(np.searchsorted(PR,around(a,True),'left')) last=len(PR) if b>=LOGCUT else int(np.searchsorted(PR,around(b,False),'right')) ls=[D.from_float(float(np.min(lo[first:last])))] if last>first else [] # Value just before the moving interval's right edge. Lower the included prime set. br=min(b,LOGCUT);j=int(np.searchsorted(PR,around(br,True),'left')) ls.append(D(int(ds[j])-len(PR))/PFX-br.ln()) if b>=LOGCUT:ls.append(cmin) upper=cmax if high_beyond else max(cmax,D.from_float(float(np.max(high[first:])))) return max(D(0),min(ge,upper-min(ls)))+D('1e-35')