Fibonacci sequence: Difference between revisions

m
(→‎BASIC: moved array example to its own subsection)
m (→‎{{header|BASIC}}: whitespace)
Line 179:
{{works with|FreeBASIC|0.20.0}}
===Iterative===
<lang qbasic>FUNCTION itFib (n)
FUNCTION itFib (n)
n1 = 0
n2 = 1
Line 189 ⟶ 188:
NEXT k
itFib = n1
END FUNCTION</lang>
</lang>
 
This one calculates each value once, as needed, and stores the results in an array for later retreival.
Line 235 ⟶ 233:
 
===Recursive===
<lang qbasic>FUNCTION recFib (n)
FUNCTION recFib (n)
IF (n < 2) THEN
recFib = n
Line 242 ⟶ 239:
recFib = recFib(n - 1) + recFib(n - 2)
END IF
END FUNCTION</lang>
</lang>
 
===Array (Table) Lookup===
Line 262 ⟶ 258:
PRINT fibNum(13)
PRINT fibNum(42)
'*****sample inputs*****</lang>
</lang>
 
=={{header|bc}}==
Anonymous user