Fibonacci sequence: Difference between revisions

m (FutureBasic entry moved out of the Basic group)
(2 intermediate revisions by 2 users not shown)
Line 7,399:
(0, 1)
[1 .. n]</syntaxhighlight>
 
==== As an unfold ====
Create an infinite list of integers using an unfold. The nth fibonacci number is the nth number in the list.
 
<syntaxhighlight lang="haskell">import Data.List (unfoldr)
 
fibs :: [Integer]
fibs = unfoldr (\(x, y) -> Just (x, (y, x + y))) (0, 1)
 
fib n :: Integer -> Integer
fib n = fibs !! n</syntaxhighlight>
 
=== With matrix exponentiation ===
Line 8,332 ⟶ 8,343:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .fibonacci = fn(.x) { if(.x < 2: .x ; self(.x - 1) + self(.x - 2)) }
val fibonacci = fn x:if(x < 2: x ; self(x - 1) + self(x - 2))
 
writeln map .(fibonacci, series (2..20</syntaxhighlight>))
</syntaxhighlight>
 
{{out}}
Line 10,418 ⟶ 10,431:
 
===Binary powering===
This is an efficient method (similar to the one used internally by <code>fibonacci()</code>), although running it without compilation won't give competitive speed.
<syntaxhighlight lang="parigp">fib(n)={
if(n<=0,
Line 10,491 ⟶ 10,505:
a
};</syntaxhighlight>
 
===Trigonometric===
This solution uses the complex hyperbolic sine.
<syntaxhighlight lang="parigp">fib(n)=real(2/sqrt(5)/I^n*sinh(n*log(I*(1+sqrt(5))/2)))\/1;</syntaxhighlight>
 
===Chebyshev===
1,007

edits