fibonacci

Haskell infinite recursion

血红的双手。 提交于 2019-11-26 17:21:00
问题 The following function computes the Fibonacci sequence: fib = 0 : 1 : (zipWith (+) fib (tail fib)) If we run it, we will get an infinite list, but how does the recursion work? Why does it get to print numbers on the screen if it the function keeps calling itself? I would appreciate if you could explain how the compiler manages the calls. 回答1: I've drawn a picture, which you might find helpful. Note that zipWtih op (x:xs) (y:xs) = (op x y):zipWith xs ys , which is how zipWtih appears to "move"

Finding out nth fibonacci number for very large 'n'

你说的曾经没有我的故事 提交于 2019-11-26 17:20:55
I was wondering about how can one find the nth term of fibonacci sequence for a very large value of n say, 1000000. Using the grade-school recurrence equation fib(n)=fib(n-1)+fib(n-2) , it takes 2-3 min to find the 50th term! After googling, I came to know about Binet's formula but it is not appropriate for values of n>79 as it is said here Is there an algorithm to do so just like we have for finding prime numbers? Wayne Rooney You can use the matrix exponentiation method (linear recurrence method). You can find detailed explanation and procedure in this blog. Run time is O (log n ). I don't

How to write a trait bound for adding two references of a generic type?

跟風遠走 提交于 2019-11-26 16:47:26
I have a Fibonacci struct that can be used as an iterator for anything that implements One , Zero , Add and Clone . This works great for all integer types. I want to use this struct for BigInteger types which are implemented with a Vec and are expensive to call clone() on. I would like to use Add on two references to T which then returns a new T (no cloning then). For the life of me I can't make one that compiles though... Working: extern crate num; use std::ops::Add; use std::mem; use num::traits::{One, Zero}; pub struct Fibonacci<T> { curr: T, next: T, } pub fn new<T: One + Zero>() ->

Generate a sequence of Fibonacci number in Scala [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-11-26 15:48:59
问题 This question already has answers here : What is the fastest way to write Fibonacci function in Scala? (8 answers) Closed 2 years ago . def fibSeq(n: Int): List[Int] = { var ret = scala.collection.mutable.ListBuffer[Int](1, 2) while (ret(ret.length - 1) < n) { val temp = ret(ret.length - 1) + ret(ret.length - 2) if (temp >= n) { return ret.toList } ret += temp } ret.toList } So the above is my code to generate a Fibonacci sequence using Scala to a value n . I am wondering if there is a more

Fibonacci sequence in Ruby (recursion)

僤鯓⒐⒋嵵緔 提交于 2019-11-26 13:08:15
问题 I\'m trying to implement the following function, but it keeps giving me the stack level too deep (SystemStackError) error. Any ideas what the problem might be ? def fibonacci( n ) [ n ] if ( 0..1 ).include? n ( fibonacci( n - 1 ) + fibonacci( n - 2 ) ) if n > 1 end puts fibonacci( 5 ) 回答1: Try this def fibonacci( n ) return n if ( 0..1 ).include? n ( fibonacci( n - 1 ) + fibonacci( n - 2 ) ) end puts fibonacci( 5 ) # => 5 check this post too Fibonacci One-Liner and more .. https://web.archive

Test if a number is fibonacci

╄→尐↘猪︶ㄣ 提交于 2019-11-26 12:50:17
问题 I know how to make the list of the Fibonacci numbers, but i don\'t know how can i test if a given number belongs to the fibonacci list - one way that comes in mind is generate the list of fib. numbers up to that number and see if it belongs to the array, but there\'s got to be another, simpler and faster method. Any ideas ? 回答1: A very nice test is that N is a Fibonacci number if and only if 5 N^2 + 4 or 5N^2 – 4 is a square number. For ideas on how to efficiently test that a number is square

Java recursive Fibonacci sequence

房东的猫 提交于 2019-11-26 11:33:53
Please explain this simple code: public int fibonacci(int n) { if(n == 0) return 0; else if(n == 1) return 1; else return fibonacci(n - 1) + fibonacci(n - 2); } I'm confused with the last line especially because if n = 5 for example, then fibonacci(4) + fibonacci(3) would be called and so on but I don't understand how this algorithm calculates the value at index 5 by this method. Please explain with a lot of detail! In fibonacci sequence each item is the sum of the previous two. So, you wrote a recursive algorithm. So, fibonacci(5) = fibonacci(4) + fibonacci(3) fibonacci(3) = fibonacci(2) +

Ruby Fibonacci algorithm

烈酒焚心 提交于 2019-11-26 11:29:15
问题 The following is a method I wrote to calculate a value in the Fibonacci sequence: def fib(n) if n == 0 return 0 end if n == 1 return 1 end if n >= 2 return fib(n-1) + (fib(n-2)) end end It works uptil n = 14, but after that I get a message saying the program is taking too long to respond (I\'m using repl.it). Anyone know why this is happening? 回答1: Naive fibonacci makes a lot of repeat calculations - in fib(14) fib(4) is calculated many times. You can add memoization to your algorithm to make

Understanding a recursively defined list (fibs in terms of zipWith)

痞子三分冷 提交于 2019-11-26 10:19:37
问题 I\'m learning Haskell, and came across the following code: fibs = 0 : 1 : zipWith (+) fibs (tail fibs) which I\'m having a bit of trouble parsing, in terms of how it works. It\'s very neat, I understand that nothing more is needed, but I\'d like to understand how Haskell manages to \"fill in\" fibs when I write: take 50 fibs Any help? Thanks! 回答1: I'll give a bit of an explanation of how it works internally. First, you must realise that Haskell uses a thing called a thunk for its values. A

Python Fibonacci Generator

删除回忆录丶 提交于 2019-11-26 08:27:40
问题 I need to make a program that asks for the amount of Fibonacci numbers printed and then prints them like 0, 1, 1, 2... but I can\'t get it to work. My code looks the following: a = int(raw_input(\'Give amount: \')) def fib(): a, b = 0, 1 while 1: yield a a, b = b, a + b a = fib() a.next() 0 for i in range(a): print a.next(), 回答1: I would use this method: Python 2 a = int(raw_input('Give amount: ')) def fib(n): a, b = 0, 1 for _ in xrange(n): yield a a, b = b, a + b print list(fib(a)) Python 3