fibonacci

How to write a generator class?

元气小坏坏 提交于 2019-11-27 03:45:18
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 value self.a not getting printed? Also, how do I write unittest for generators? How to write a

Python Fibonacci Generator

浪尽此生 提交于 2019-11-27 01:58:57
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(), 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 a = int(input('Give amount: ')) def fib(n): a, b = 0, 1 for _ in range(n): yield a a, b = b, a + b print(list

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

心不动则不痛 提交于 2019-11-27 01:26:08
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) ? 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⋅...⋅2 = 2ⁿ bonus : The best theoretical implementation to fibonacci is actually a close formula , using the

Fast Fibonacci recursion

你。 提交于 2019-11-27 00:23:29
问题 I'm trying to recall an algorithm on Fibonacci recursion. The following: public int fibonacci(int n) { if(n == 0) return 0; else if(n == 1) return 1; else return fibonacci(n - 1) + fibonacci(n - 2); } is not what I'm looking for because it's greedy. This will grow exponentially (just look at Java recursive Fibonacci sequence - the bigger the initial argument the more useless calls will be made). There is probably something like a "cyclic argument shift", where calling previous Fibonacci value

Recursive Fibonacci

放肆的年华 提交于 2019-11-27 00:14:15
问题 I'm having a hard time understanding why #include <iostream> using namespace std; int fib(int x) { if (x == 1) { return 1; } else { return fib(x-1)+fib(x-2); } } int main() { cout << fib(5) << endl; } results in a segmentation fault. Once x gets down to 1 shouldn't it eventually return? 回答1: When x==2 you call fib(1) and fib(0) : return fib(2-1)+fib(2-2); Consider what will happen when fib(0) is evaluated... 回答2: The reason is because Fibonacci sequence starts with two known entities, 0 and 1

How does the fibonacci recursive function “work”?

给你一囗甜甜゛ 提交于 2019-11-26 23:44:13
I'm new to Javascript and was reading up on it, when I came to a chapter that described function recursion. It used an example function to find the nth number of the Fibonacci sequence. The code is as follows: function fibonacci(n) { if (n < 2){ return 1; }else{ return fibonacci(n-2) + fibonacci(n-1); } } console.log(fibonacci(7)); //Returns 21 I'm having trouble grasping exactly what this function is doing. Can someone explain what's going on here? I'm getting stuck on the 5th line, where the function calls itself. What's happening here? You're defining a function in terms of itself. In

An iterative algorithm for Fibonacci numbers

六眼飞鱼酱① 提交于 2019-11-26 22:31:36
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 The problem is that your return y is within the loop of your function. So after the first iteration, it will already stop and return the first

Generating Fibonacci sequence

泪湿孤枕 提交于 2019-11-26 22:00:40
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? Rob W 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; fib[1] = 1; for (i = 2; i <= 10; i++) { // Next fibonacci number = previous + one before previous //

Can a Fibonacci function be written to execute in O(1) time?

☆樱花仙子☆ 提交于 2019-11-26 20:49:58
问题 So, we see a lot of fibonacci questions. I, personally, hate them. A lot. More than a lot. I thought it'd be neat if maybe we could make it impossible for anyone to ever use it as an interview question again. Let's see how close to O(1) we can get fibonacci. Here's my kick off, pretty much crib'd from Wikipedia, with of course plenty of headroom. Importantly, this solution will detonate for any particularly large fib, and it contains a relatively naive use of the power function, which places

Count number of possible paths up ladder

一世执手 提交于 2019-11-26 20:37:04
问题 I can't seem to come up with an algorithm to solve the following problem, I tried using a series of for-loops but it became way too complicated: A ladder has n steps, one can climb the ladder using any combination of steps of 1 or steps of 2. How many possible ways are there for one to climb the ladder? So for example, if the ladder had 3 steps , these would be the possible paths: 1-1-1 2-1 1-2 And for 4 steps 1-1-1-1 2-1-1 1-2-1 1-1-2 2-2 Any insight as to how this could be done would be