Integers in Base Pi
Python 3, 471 317 310 bytes
7 bytes thanks to caird coinheringaahing.
Surely there are golfs that I missed. Feel free to point them out in the comments.
def h(Q):
a=0;C=b=4;c=d=s=1;P=o=3
while P^C:
a,b,c,d,s,o,P,A,B=b,s*a+o*b,d,s*c+o*d,s+o,o+2,C,0,1
for I in Q:B*=c;A=A*a+I*B
C=A>0
return P
def f(n,p):
Q=[-n];R=""
while h([1]+Q)<1:Q=[0]+Q
Q+=[0]*p
for I in range(len(Q)):
i=3;Q[I]+=3
while h(Q):Q[I]-=1;i-=1
R+=str(i)
return R[:-p]+"."+R[-p:]
Try it online!
Ungolfed version:
def poly_eval_pi_pos(poly,a=0,b=4,c=1,d=1,o=3,s=1,prev=9,curr=6):
while prev != curr:
a,b,c,d,s,o=b,s*a+o*b,d,s*c+o*d,s+o,o+2
prev = curr
res_n, res_d = 0,1
for I in poly:
res_d *= c
res_n = res_n*a + I * res_d
curr = res_n > 0
return prev
def to_base_pi(n,precision):
poly = [-n]
res = ""
while not poly_eval_pi_pos([1]+poly):
poly = [0]+poly
poly += [0]*precision
for index in range(len(poly)):
i = 3
poly[index] += 3
while poly_eval_pi_pos(poly):
poly[index] -= 1
i -= 1
res += str(i)
return res[:-precision]+"."+res[-precision:]
Try it online!
Ruby -rbigdecimal/math
, 111 103 97 bytes
->x,n{q=BigMath::PI n;r=q**m=Math.log(x,q).to_i;n.times{$><<"#{?.if-2==m-=1}%i"%d=x/r;x%=r;r/=q}}
Try it online!
Takes input number as x
and desired precision as n
. Outputs by printing. Makes use of the built-in BigDecimal library for arbitrary pecision PI value.