RosettaCodeData/Task/Van-der-Corput-sequence/Phix/van-der-corput-sequence.phix

45 lines
1.0 KiB
Plaintext

enum BASE, FRAC, DECIMAL
constant DESC = {"Base","Fraction","Decimal"}
function vdc(integer n, atom base, integer flag)
object res = ""
atom num = 0, denom = 1, digit, g
while n do
denom *= base
digit = remainder(n,base)
n = floor(n/base)
if flag=BASE then
res &= digit+'0'
else
num = num*base+digit
end if
end while
if flag=FRAC then
g = gcd(num,denom)
return {num/g,denom/g}
elsif flag=DECIMAL then
return num/denom
end if
return {iff(length(res)=0?"0":"0."&res)}
end function
procedure show_vdc(integer flag, string fmt)
object v
for i=2 to 5 do
printf(1,"%s %d: ",{DESC[flag],i})
for j=0 to 9 do
v = vdc(j,i,flag)
if flag=FRAC and v[1]=0 then
printf(1,"0 ")
else
printf(1,fmt,v)
end if
end for
puts(1,"\n")
end for
end procedure
show_vdc(BASE,"%s ")
show_vdc(FRAC,"%d/%d ")
show_vdc(DECIMAL,"%g ")