fibonacci

Numpy's matrix_power function giving wrong results for large exponents [duplicate]

我怕爱的太早我们不能终老 提交于 2019-12-10 16:49:43
问题 This question already has an answer here : Numpy matrix exponentiation gives negative value (1 answer) Closed 2 years ago . I'm working on an implementation of the Fibonacci sequence in Numpy using the Q-Matrix method. The results are fine up till n = 47. At this point, the matrix_power function is returning incorrect results. Any explanation about why this is happening? import numpy def fibonacci(n): qmatrix = numpy.matrix([[1, 1], [1, 0]]) (a,b,c,d) = numpy.linalg.matrix_power(qmatrix,n)

Why this implementation of Fibonacci is extremely fast?

谁说胖子不能爱 提交于 2019-12-10 15:27:07
问题 This implementation of Fibonacci is easy to understand but very slow: fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib (n-2) Following implementation of Fibonacci is hard to understand but super fast. It calculates 100,000th Fibonacci number instantly on my laptop. fib = fastFib 1 1 fastFib _ _ 0 = 0 fastFib _ _ 1 = 1 fastFib _ _ 2 = 1 fastFib a b 3 = a + b fastFib a b c = fastFib (a + b) a (c - 1) What the magic is happening here about the latter implementation and how does it work? 回答1: Why is

Fibonacci sequence generation

岁酱吖の 提交于 2019-12-10 15:14:15
问题 I was writing a fibonacci sequence generator, and I was trying to understand the following code in Haskell fibs = 1 : 1 : zipWith (+) fibs (tail fibs) I understand what zipWith is, but I do not exactly know how the program executes and why it does generate all the fibonacci numbers. I was trying to understand why it does not terminate using the environment concept in functional languages as follows: Initially, because Haskell's lazy evaluation, the binding in the env should be fibs : [1,1,x]

Fast Computation of Pisano Period

狂风中的少年 提交于 2019-12-10 11:58:47
问题 I want to compute the Pisano Period of a number m in less than 1s. This is the code I currently have in C++ : #include <iostream> #include <vector> using std::vector; bool is_equal(vector<long long> v, long k) { if (k == 0) return false; // compare first and second half of array for (long i = 0, j = k; i < k, j < v.size(); ++i, ++j) { if (v[i] != v[j]) return false; } return true; } long long get_pisano_period(long long m) { vector<long long> v; long long a = 0; long k = 0; long long b = 1; /

Computing the nth Fibonacci number using linear recursion [duplicate]

吃可爱长大的小学妹 提交于 2019-12-10 11:43:10
问题 This question already has answers here : Java arrays printing out weird numbers and text [duplicate] (10 answers) Closed last month . I have tried binary recursion to find the nth Fibonacci number (or the whole Fibonacci series by using a for loop in main() ) but according to Data Structures and Algorithms in Java (6th Edition) by Michael T. Goodrich; it is a terribly inefficient method as it requires an exponential number of calls to the method. An efficient recursion technique is linear

Python: Creating a List of the First n Fibonacci Numbers [duplicate]

ε祈祈猫儿з 提交于 2019-12-10 11:09:57
问题 This question already has answers here : How to write the Fibonacci Sequence? (44 answers) Closed 4 years ago . I am new to Python and to these forums. My question is: How can I create a list of n Fibonacci numbers in Python? So far, I have a function that gives the nth Fibonacci number, but I want to have a list of the first n Fib. numbers for future work. For example: fib(8) -> [0,1,1,2,3,5,8,13] 回答1: Try this, a recursive implementation that returns a list of numbers by first calculating

What can I do to improve my Fibonacci number generator?

不打扰是莪最后的温柔 提交于 2019-12-10 10:54:18
问题 I'm solving this problem: G(n) is defined as G(n) = G(n-1) + f(4n-1) , for n > 0 and G(0) = 0 f(i) is ith Fibonacci number. Given n you need to evaluate G(n) modulo 1000000007. Input First line contains number of test cases t (t<40000). Each of the next t lines contain an integer n ( 0 <= n < 2^51). Output For each test case print G(n) modulo 1000000007. Example Input: 2 2 4 Output: 15 714 This is the code I've written: typedef long long type; #define check 1000000007 type x; type y; type f

Sum of Fibonacci numbers

Deadly 提交于 2019-12-10 08:07:16
问题 I'm rather new to Haskell. The problem is to find the sum of all even Fibonacci numbers not greater than 4 million. I can't use lists. If I understand correctly, the below solution is wrong, because it uses lists: my_sum = sum $ filter (odd) $ takeWhile (< 4000000) fibs Where fibs is the list of all Fibonacci numbers. Somehow, I find it difficult not to think in Haskell in terms of lists. Could anyone guide me to a solution to this problem? Regards EDIT: If anyone is interested, I've solved

Python variable assignment question

喜欢而已 提交于 2019-12-10 06:18:10
问题 a,b = 0,1 while b < 50: print(b) a = b b = a+b outputs: 1 2 4 8 16 32 wheras: a,b = 0,1 while b < 50: print(b) a,b = b, a+b outputs (correct fibonacci sequence): 1 1 2 3 5 8 13 21 34 Aren't they the same? I mean a,b = b, a+b is essentially the same as a = b and b = a+b written separately -- no? 回答1: No, they are not the same. When you write a,b = b, a+b , the assignments are done "simultaneously". a,b = b, a+b is same as (a, b) = (b, a+b) . So, after a, b = 5, 8 a=5 and b=8. When Python sees

C print first million Fibonacci numbers

。_饼干妹妹 提交于 2019-12-10 04:00:00
问题 I am trying to write C code which will print the first 1million Fibonacci numbers. The actual problem is I want to get the lats 10 digits of F(1,000,000) I understand how the sequence works and how to write the code to achieve that however as F(1,000,000) is very large I am struggling to find a way to represent it. This is code I am using: #include<stdio.h> int main() { unsigned long long n, first = 0, second = 1, next, c; printf("Enter the number of terms\n"); scanf("%d",&n); printf("First