(phixonline)-->
with javascript_semantics
sequence row, rows = {}
function check(integer i, string name) -- helper routine
string s = join(row,fmt:="%d")
if s!=rows[i+1] then
printf(1,"%s: goes wrong on line %d\n",{name,i+1})
return false
end if
return true
end function
procedure sum_prior() -- First method
row = {} -- (using just one row)
for i=0 to 119 do
row &= 1
for n=length(row)-1 to 2 by -1 do
row[n] += row[n-1]
end for
if not check(i,"sum_prior") then exit end if
end for
end procedure
procedure algebraic() -- Second method
for i=0 to 119 do
atom c = 1
row = {1}
for j=0 to i-1 do
-- c *= (i-j)/(j+1) -- NO: precedence differs, and eg 5*(1/5) inexact
c = c*(i-j)/(j+1) -- ..whereas (5*1)/5 is exact
row &= c
end for
if not check(i,"algebraic") then exit end if
end for
end procedure
-- Aside: error with c was self-evident and more relevant when it was an integer,
--- but I made it an atom to get past the 33/63 limits that imposed.
procedure builtin() -- Third method
for i=0 to 119 do
row = {}
for j=0 to i do
row &= choose(i,j)
end for
if not check(i,"builtin") then exit end if
end for
end procedure
include mpfr.e
procedure arbitrary_precision() -- Fourth method, but run first
-- perfectly accurate until you
-- run out of memory or patience.
mpz z = mpz_init()
for i=0 to 119 do
row = {}
for j=0 to i do
mpz_bin_uiui(z,i,j)
row = append(row,mpz_get_str(z))
end for
string s = join(row)
if i<=19 then
printf(1,"%=96s\n",s)
end if
-- to check the others against:
rows = append(rows,s)
end for
printf(1,"\n")
end procedure
arbitrary_precision()
sum_prior()
algebraic()
builtin()