• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

I hate you, Fibonacci!

I took me forever to write a simple program using Maple that would sum the first n terms of the Fibonacci sequence. My programming skills suck. 😱 Would anyone have done it differently?

EDIT: I apologzie for the lack of spacing but FuseTalk sucks at spacing.

Fibon:=proc(n)
local s, F1, F2, F3, k;

if n=1 then
print(0);

elif n=2 then
print(1);

else
s:=1;
F1:=0;
F2:=1;
for k from 3 to n do
s:=s+F1+F2;
F3:=F1+F2
F1:=F2;
F2:=F3;

end do;

print(s);

end if;

end proc;
 
if i remember correctly, you can do this as a recursive program, which will save on the amount of code -- however, may not be as computationally efficient.
 
You did an iterative version which runs linear runtime. A recursive version is somewhat shorter, but has exponential runtime. Just stick at what you have right now.
 
Back
Top