fibonacci

What is the fastest way to write Fibonacci function in Scala?

不想你离开。 提交于 2019-12-17 09:48:18
问题 I've looked over a few implementations of Fibonacci function in Scala starting from a very simple one, to the more complicated ones. I'm not entirely sure which one is the fastest. I'm leaning towards the impression that the ones that uses memoization is faster, however I wonder why Scala doesn't have a native memoization. Can anyone enlighten me toward the best and fastest (and cleanest) way to write a fibonacci function? 回答1: for me the simplest defines a recursive inner tail function: def

An inverse Fibonacci algorithm?

两盒软妹~` 提交于 2019-12-17 08:12:31
问题 There are dozens of ways of computing F(n) for an arbitrary n, many of which have great runtime and memory usage. However, suppose I wanted to ask the opposite question: Given F(n) for n > 2, what is n? (The n > 2 restriction is in there since F(1) = F(2) = 1 and there's no unambiguous inverse). What would be the most efficient way of solving this problem? It's easy to do this in linear time by enumerating the Fibonacci numbers and stopping when you hit the target number, but is there some

An inverse Fibonacci algorithm?

让人想犯罪 __ 提交于 2019-12-17 08:12:23
问题 There are dozens of ways of computing F(n) for an arbitrary n, many of which have great runtime and memory usage. However, suppose I wanted to ask the opposite question: Given F(n) for n > 2, what is n? (The n > 2 restriction is in there since F(1) = F(2) = 1 and there's no unambiguous inverse). What would be the most efficient way of solving this problem? It's easy to do this in linear time by enumerating the Fibonacci numbers and stopping when you hit the target number, but is there some

Why is the complexity of computing the Fibonacci series 2^n and not n^2?

青春壹個敷衍的年華 提交于 2019-12-17 06:41:22
问题 I am trying to find complexity of Fibonacci series using a recursion tree and concluded height of tree = O(n) worst case, cost of each level = cn , hence complexity = n*n=n^2 How come it is O(2^n) ? 回答1: The complexity of a naive recursive fibonacci is indeed 2ⁿ. T(n) = T(n-1) + T(n-2) = T(n-2) + T(n-3) + T(n-3) + T(n-4) = = T(n-3) + T(n-4) + T(n-4) + T(n-5) + T(n-4) + T(n-5) + T(n-5) + T(n-6) = ... In each step you call T twice, thus will provide eventual asymptotic barrier of: T(n) = 2⋅2⋅..

Generating Fibonacci numbers in Haskell?

感情迁移 提交于 2019-12-17 06:27:34
问题 In Haskell, how can I generate Fibonacci numbers based on the property that the nth Fibonacci number is equal to the (n-2)th Fibonacci number plus the (n-1)th Fibonacci number? I've seen this: fibs :: [Integer] fibs = 1 : 1 : zipWith (+) fibs (tail fibs) I don't really understand that, or how it produces an infinite list instead of one containing 3 elements. How would I write haskell code that works by calculating the actual definition and not by doing something really weird with list

An iterative algorithm for Fibonacci numbers

不打扰是莪最后的温柔 提交于 2019-12-17 06:11:21
问题 I am interested in an iterative algorithm for Fibonacci numbers, so I found the formula on wiki...it looks straight forward so I tried it in Python...it doesn't have a problem compiling and formula looks right...not sure why its giving the wrong output...did I not implement it right ? def fib (n): if( n == 0): return 0 else: x = 0 y = 1 for i in range(1,n): z = (x + y) x = y y = z return y for i in range(10): print (fib(i)) output 0 None 1 1 1 1 1 1 回答1: The problem is that your return y is

An iterative algorithm for Fibonacci numbers

Deadly 提交于 2019-12-17 06:11:10
问题 I am interested in an iterative algorithm for Fibonacci numbers, so I found the formula on wiki...it looks straight forward so I tried it in Python...it doesn't have a problem compiling and formula looks right...not sure why its giving the wrong output...did I not implement it right ? def fib (n): if( n == 0): return 0 else: x = 0 y = 1 for i in range(1,n): z = (x + y) x = y y = z return y for i in range(10): print (fib(i)) output 0 None 1 1 1 1 1 1 回答1: The problem is that your return y is

Generating Fibonacci sequence

左心房为你撑大大i 提交于 2019-12-17 05:48:09
问题 var x=0, var y=1; var z; fib[0] = 0; fib[1] = 1; for(i=2; i<=10; i++) { alert(x+y); fib[i]=x+y; x=y; z=y; } I'm trying to get to generate a simple Fibonacci sequence but there no output. Can anybody let me know what's wrong? 回答1: You have never declared fib to be an array. Use var fib = []; to solve this. Also, you're never modifying the y variable, neither using it. The code below makes more sense, plus, it doesn't create unused variables: var i; var fib = []; // Initialize array! fib[0] = 0

How to write a generator class?

女生的网名这么多〃 提交于 2019-12-17 04:56:09
问题 I see lot of examples of generator functions, but I want to know how to write generators for classes. Lets say, I wanted to write Fibonacci series as a class. class Fib: def __init__(self): self.a, self.b = 0, 1 def __next__(self): yield self.a self.a, self.b = self.b, self.a+self.b f = Fib() for i in range(3): print(next(f)) Output: <generator object __next__ at 0x000000000A3E4F68> <generator object __next__ at 0x000000000A3E4F68> <generator object __next__ at 0x000000000A3E4F68> Why is the

Fibonacci in Python - Simple solution [duplicate]

流过昼夜 提交于 2019-12-14 03:32:25
问题 This question already has answers here : How to write the Fibonacci Sequence? (44 answers) Closed last year . n1 = 1 n2 = 1 n3 = n1 + n2 for i in range(10): n1 + n2 print(n3) n1 = n2 n2 = n3 According to what I know, this should be the simplest way of outputting the first 10 digits of the series, however, it prints 2 10 times. I don't understand why n1 doesn't get set to n2 , and n2 doesn't get set to n3 after n3 has been printed. 回答1: There are many issues with your code. And you should