fibonacci

Printing out the fibonacci series

大兔子大兔子 提交于 2020-01-21 08:56:27
问题 I am trying to write a simple Python program. It's supposed to return a a closure that returns successive fibonacci numbers: def fibGen(): n_1 = 0 n_2 = 0 n = 1 def fib(): if n_1 ==0 and n_2 ==0: n_1 = 1 return n else: n = n_1 + n_2 n_2 = n_1 n_1 = n return n return fib f = fibGen() for i in range(0,10): print(f()) I get this error at run time: UnboundLocalError: local variable 'n_1' referenced before assignment EDIT: In my original post, I had not included n = 1 in the definition of fibGen

Print nth Fibonacci number [upto 1000 digits] [closed]

断了今生、忘了曾经 提交于 2020-01-17 14:43:48
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . How can one calculate nth Fibonacci number in C/C++ ? The Fibonacci number can contain upto 1000 digits. I can generate numbers upto 2^64 (unsigned long long). Since that is the limit of numbers, so I am guessing there has to be some other way of doing it - which I do not know of. EDIT: Also, it has to be done

Generating a list of EVEN numbers in Python

点点圈 提交于 2020-01-14 07:30:07
问题 Basically I need help in generating even numbers from a list that I have created in Python: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, ...] I have tried a couple different methods, but every time I print, there are odd numbers mixed in with the evens! I know how to generate even/odd numbers if I were to do a range of 0-100, however, getting only the even numbers from the previous mentioned list has me stumped! P.S. I've

Fibonacci Sequence (JS) - Sum of Even Numbers

折月煮酒 提交于 2020-01-13 09:25:37
问题 I started Project Euler. I am on problem 2 and came up with this code to come up with the sum of even fibonacci numbers up to 4 million. The code seems to do pretty much what I want it to. I do see the correct sum listed when the code is ran. The only part I am really confused about is the very last number displayed in the results. This is what it shows: JS CODE: var previous = 0; var current = 1; var sum = 0; var next; for(i = 1; i < 100; i++){ next = current + previous; previous = current;

Iterative Version of Modified Fibonacci Sequence

放肆的年华 提交于 2020-01-13 06:32:10
问题 I was just going through the iterative version of fibonacci series algorithm. I found this following code int Fibonacci(int n) { int f1 = 0; int f2 = 1; int fn; for ( int i = 2; i < n; i++ ) { fn = f1 + f2; f1 = f2; f2 = fn; } } A silly question just raised in my mind. The function above adds two previous numbers and returns the third one and then get variables ready for the next iteration. What if it would be something like this. "Return a number of series which is the sum of previous three

fibonacci series - recursive summation

风格不统一 提交于 2020-01-12 05:35:28
问题 Ok, I initially wrote a simple code to return the Fibonacci number from the series based on the user input.. n=5 will produce 3.. static int fibonacci(int n) { if (n == 1) return 0; else if (n == 2) return 1; else return (fibonacci(n - 1) + fibonacci(n - 2)); } I was thinking of modifying the code to return the sum of the series rather than just returning the value from the series and while trying to do the sum I accidentally added 1 to the return statement and to my surprise, it returned the

Finding Fibonacci sequence in C#. [Project Euler Exercise]

末鹿安然 提交于 2020-01-12 04:07:50
问题 I'm having some trouble with this problem in Project Euler. Here's what the question asks: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Find the sum of all the even-valued terms in the sequence which do not exceed four million. My code so far: EDITED WITH NEW CODE THAT STILL DOESN'T WORK. static void Main(string[] args) { int a = 1; int b = 2; int Container =

Fibonacci Series in Assembly x86

泪湿孤枕 提交于 2020-01-11 08:27:50
问题 Finally after a long session of countless errors , hope this is the final one. No Compile or runtime errors, Just a logical error. EDIT: (Fixed Pseudocode) My Pseudocode: first = 1; second = 1; third = 0; for i from 1 to n{ third=first+second first=second second=third } return third This would print the final result of the series. My Assembly Code: I have added Comments where ever possible .386 .model flat,stdcall option casemap:none .data timestell db "Loop Ran : %d Times -----",0 ;format

Recursive Fibonacci Function (With Negative Numbers)

给你一囗甜甜゛ 提交于 2020-01-06 09:16:16
问题 I am able to write a recursive Fibonacci function for all numbers greater than 0, but the function is completely incorrect for anything negative. Any idea how to implement this in c++? int fibonacci(int n){ if(n == 0)return 0; if(n == 1)return 1; return fibonacci(n - 1) + fibonacci(n - 2); } 回答1: According to wikipedia, http://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers, the recursive function for negative numbers is different than for possitive numbers. For possitive: n_2 = n

Recursive Fibonacci Function (With Negative Numbers)

点点圈 提交于 2020-01-06 09:10:08
问题 I am able to write a recursive Fibonacci function for all numbers greater than 0, but the function is completely incorrect for anything negative. Any idea how to implement this in c++? int fibonacci(int n){ if(n == 0)return 0; if(n == 1)return 1; return fibonacci(n - 1) + fibonacci(n - 2); } 回答1: According to wikipedia, http://en.wikipedia.org/wiki/Generalizations_of_Fibonacci_numbers, the recursive function for negative numbers is different than for possitive numbers. For possitive: n_2 = n