RosettaCodeData/Task/Fibonacci-sequence/Phix/fibonacci-sequence-1.phix

17 lines
457 B
Text
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
function fibonacci(integer n) -- iterative, works for -ve numbers
atom a=0, b=1
2016-12-05 23:44:36 +01:00
if n=0 then return 0 end if
2017-09-23 10:01:46 +02:00
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
2016-12-05 23:44:36 +01:00
if n<0 and remainder(n,2)=0 then return -fcache[absn] end if
return fcache[absn]
end function
2017-09-23 10:01:46 +02:00
for i=0 to 28 do
if i then puts(1,", ") end if
printf(1,"%d", fibonacci(i))
2016-12-05 23:44:36 +01:00
end for
puts(1,"\n")