3 ms·
A fib sequence can be defined corecursively using unfold and its actually a nice way to write it in some languages. Here is the next fib in a sequence (in psue
by adamgordonbell 5y ago
A fib sequence can be defined corecursively using unfold and its actually a nice way to write it in some languages.
Here is the next fib in a sequence (in psuedocode):
(a, b) -> (a, (b, a+b))
And then you can get a sequence with unfold by giving it some starting numbers:
val fibonacci: Iterator[Int] =
Iterator.unfold((1, 1)) {
case (x, y) => Some((x, (y, x + y)))
}
(That is Scala. But lots of languages have an unfold method)
> fibonacci.take(8).toList
1, 1, 2, 3, 5, 8, 13, 21
- agumonkey 5y ago(a, b) -> (a, (b, a+b)) funny it's almost graphically equivalent to the geometric ratio
- zuminator 5y agoIt's not almost equivalent to the geometric ratio, it is the geometric (aka golden) ratio, at the limit, as first observed by none other than Johannes Kepler [0]. You are in heavenly company! [0]https://en.wikipedia.org/wiki/Fibonacci_number#Limit_of_consecutive_quotients https://en.wikipedia.org/wiki/Fibonacci_number#Limit_of_cons...