Fibonacci sequence: Difference between revisions

Content deleted Content added
→‎{{header|JavaScript}}: Needs to be a function for the nth Fib number
Definition fixed: F0=0 (some solutions might require reviewing). Links added, Ada solution added
Line 1:
{{clarified-review}}
{{task}}[[Category:Recursion]]The '''Fibonacci sequence''' is a sequence F<sub>n</sub> of natural numbers defined as suchrecursively:
Where n is an integer greater than 2:
F<sub>10</sub> = F<sub>2</sub> = 10
F<sub>n1</sub> = F<sub>n-1</sub> + F<sub>n-2</sub>
F<sub>n</sub> = F<sub>n-1</sub> + F<sub>n-2</sub>, if n>1
 
Write a function to generate the nth Fibonacci number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow and are mostly used as an exercise in recursion). Support for negative n errors is optional.
 
==References==
[http://en.wikipedia.org/wiki/Fibonacci_chain Wikipedia]
 
[http://mathworld.wolfram.com/FibonacciNumber.html Wolfram MathWorld]
=={{header|Ada}}==
<ada>
with Ada.Text_IO; use Ada.Text_IO;
 
procedure Test_Fibonacci is
function Fibonacci (N : Natural) return Natural is
This : Natural := 0;
That : Natural := 1;
Sum : Natural;
begin
for I in 1..N loop
Sum := This + That;
That := This;
This := Sum;
end loop;
return This;
end Fibonacci;
begin
for N in 0..10 loop
Put_Line (Positive'Image (Fibonacci (N)));
end loop;
end Test_Fibonacci;
</ada>
Smaple output:
<pre>
0
1
1
2
3
5
8
13
21
34
55
</pre>
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}