Fibonacci sequence: Difference between revisions
Content deleted Content added
Definition fixed: F0=0 (some solutions might require reviewing). Links added, Ada solution added |
|||
Line 380: | Line 380: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===Recursive=== |
|||
{{incorrect|JavaScript}} |
|||
One possibility familiar to Scheme programmers is to define an internal function for iteration through anonymous tail recursion: |
|||
<pre>fib = [0,1]; |
|||
<javascript> function fib(n) { |
|||
for(i = 2; i <= 20; i++) |
|||
return function(n,a,b) { |
|||
fib[i] = fib[i - 1] + fib[i - 2]; |
|||
return n>0 ? arguments.callee(n-1,b,a+b) : a; |
|||
alert(fib);</pre> |
|||
}(n,0,1); |
|||
}</javascript> |
|||
===Iterative=== |
|||
<javascript> function fib(n) { |
|||
var a=0, b=1, t; |
|||
while (n-- > 0) { |
|||
t = a; |
|||
a = b; |
|||
b += t; |
|||
} |
|||
return a; |
|||
} |
|||
for (var i=0; i<10; ++i) alert(fib(i));</javascript> |
|||
=={{header|Joy}}== |
=={{header|Joy}}== |