September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,18 +1,16 @@
|
|||
sequence fcache = {1,1}
|
||||
|
||||
function fibonamem(integer n) -- memoized, works for -ve numbers, inaccurate above 78
|
||||
integer absn = abs(n)
|
||||
function fibonacci(integer n) -- iterative, works for -ve numbers
|
||||
atom a=0, b=1
|
||||
if n=0 then return 0 end if
|
||||
if absn>length(fcache) then
|
||||
fcache = append(fcache,fibonamem(absn-1)+fibonamem(absn-2))
|
||||
if absn!=length(fcache) then ?9/0 end if
|
||||
end if
|
||||
if abs(n)>=79 then ?9/0 end if -- inaccuracies creep in above 78
|
||||
for i=1 to abs(n)-1 do
|
||||
{a,b} = {b,a+b}
|
||||
end for
|
||||
if n<0 and remainder(n,2)=0 then return -fcache[absn] end if
|
||||
return fcache[absn]
|
||||
end function
|
||||
|
||||
for i=0 to 30 do
|
||||
printf(1,"%d", fibonamem(i))
|
||||
if i!=30 then puts(1,", ") end if
|
||||
for i=0 to 28 do
|
||||
if i then puts(1,", ") end if
|
||||
printf(1,"%d", fibonacci(i))
|
||||
end for
|
||||
puts(1,"\n")
|
||||
|
|
|
|||
|
|
@ -5,18 +5,17 @@ sequence fcacheba = {BA_ONE,BA_ONE}
|
|||
function fibonamemba(integer n) -- memoized, works for -ve numbers, yields bigatom
|
||||
integer absn = abs(n)
|
||||
if n=0 then return BA_ZERO end if
|
||||
if absn>length(fcacheba) then
|
||||
fcacheba = append(fcacheba,ba_add(fibonamemba(absn-1),fibonamemba(absn-2)))
|
||||
if absn!=length(fcacheba) then ?9/0 end if
|
||||
end if
|
||||
while length(fcacheba)<absn do
|
||||
fcacheba = append(fcacheba,ba_add(fcacheba[$],fcacheba[$-1]))
|
||||
end while
|
||||
if n<0 and remainder(n,2)=0 then return ba_sub(0,fcacheba[absn]) end if
|
||||
return fcacheba[absn]
|
||||
end function
|
||||
|
||||
for i=0 to 30 do
|
||||
for i=0 to 28 do
|
||||
if i then puts(1,", ") end if
|
||||
ba_printf(1,"%B", fibonamemba(i))
|
||||
if i!=30 then puts(1,", ") end if
|
||||
end for
|
||||
puts(1,"\n")
|
||||
ba_printf(1,"%B", fibonamemba(777))
|
||||
ba_printf(1,"%B", fibonamemba(705))
|
||||
puts(1,"\n")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue