fibonacci

Is there something wrong with this python code, why does it run so slow compared to ruby?

橙三吉。 提交于 2019-12-01 07:39:23
I was interested in comparing ruby speed vs python so I took the simplest recursive calculation, namely print the fibonacci sequance. This is the python code #!/usr/bin/python2.7 def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1)+fib(n-2) i = 0 while i < 35: print fib(i) i = i + 1 and here is the ruby code #!/usr/bin/ruby def fib(n) if n == 0 return 0 elsif n == 1 return 1 else fib(n-1)+fib(n-2) end end i = 0 while (i < 35) puts fib(i) i = i + 1 end over several runs, time reports this average real 0m4.782s user 0m4.763s sys 0m0.010s thats for ruby, now python2.7 gives

Fibonacci Sequence in Java taking too long?

牧云@^-^@ 提交于 2019-12-01 06:27:18
I'm trying to find the sum of the Fibonacci sequence in Java, but the run time is taking way too long (or is it suppose to?). This slows down anytime I use an integer past 40. Note: At 50, a negative value is returned which boggles my mind. Any advice? public static void main(String[] args) { //Find Fibonacci sequence int sum=getSum(50); System.out.println("Sum of Fibonacci Numbers is " + sum); } static int getSum(int n){ if (n==0) return 0; if (n==1 || n==2) return 1; else return getSum(n-1) + getSum(n-2); } For n > 2 , an invocation of your getSum(n) recursively invokes itself twice. Each of

Calculating Fibonacci Numbers Recursively in C

我的未来我决定 提交于 2019-12-01 06:25:48
问题 I'm trying to learn C by writing a simple program to output Fibonacci numbers. It isn't working. fibonacci.h unsigned int fibonacci_recursive(unsigned int n); fibonacci.c #include <stdio.h> #include "fibonacci.h" main() { unsigned int i; for (i = 0; i < 10; i++) { printf("%d\t%n", fibonacci_recursive(i)); } getchar(); } fibonacci_recursive.c unsigned int fib_rec(unsigned int n); main(unsigned int n) { return fib_rec(n); } unsigned int fib_rec(unsigned int n) { if (n == 0) { return 0; } if (n

Is there something wrong with this python code, why does it run so slow compared to ruby?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 05:15:19
问题 I was interested in comparing ruby speed vs python so I took the simplest recursive calculation, namely print the fibonacci sequance. This is the python code #!/usr/bin/python2.7 def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1)+fib(n-2) i = 0 while i < 35: print fib(i) i = i + 1 and here is the ruby code #!/usr/bin/ruby def fib(n) if n == 0 return 0 elsif n == 1 return 1 else fib(n-1)+fib(n-2) end end i = 0 while (i < 35) puts fib(i) i = i + 1 end over several runs,

Fibonacci Sequence in Java taking too long?

一曲冷凌霜 提交于 2019-12-01 05:01:39
问题 I'm trying to find the sum of the Fibonacci sequence in Java, but the run time is taking way too long (or is it suppose to?). This slows down anytime I use an integer past 40. Note: At 50, a negative value is returned which boggles my mind. Any advice? public static void main(String[] args) { //Find Fibonacci sequence int sum=getSum(50); System.out.println("Sum of Fibonacci Numbers is " + sum); } static int getSum(int n){ if (n==0) return 0; if (n==1 || n==2) return 1; else return getSum(n-1)

Function-local, self-referential, lazy fibonacci sequence

为君一笑 提交于 2019-12-01 04:12:42
问题 I would like to create a function that returns a lazily extended infinite sequence of Fibonacci numbers. Right now, I can make my sequence available in the top-level namespace like this: (def fibonacci-numbers (lazy-cat [0 1] (map + fibonacci-numbers (rest fibonacci-numbers)))) However, this means that if I start consuming a lot of them, I lose control over the garbage collection. I am looking to do something like: (defn fibonacci-numbers-fn [] (lazy-cat [0 1] (map + (fibonacci-numbers-fn)

Numpy matrix exponentiation gives negative value

℡╲_俬逩灬. 提交于 2019-12-01 03:55:41
问题 I wanted to use NumPy in a Fibonacci question because of its efficiency in matrix multiplication. You know that there is a method for finding Fibonacci numbers with the matrix [[1, 1], [1, 0]] . I wrote some very simple code but after increasing n , the matrix is starting to give negative numbers. import numpy def fib(n): return (numpy.matrix("1 1; 1 0")**n).item(1) print fib(90) # Gives -1581614984 What could be the reason for this? Note: linalg.matrix_power also gives negative values. Note2

Javascript Fibonacci

ぐ巨炮叔叔 提交于 2019-11-30 22:37:59
function fibo() { var first,second,add; for(var i=0;i<4000;i++){ if(i === 0){ first = 1; second = 2; } add = first + second; first = second; second = add; } alert(add); } fibo(); not working shows infinity why? Simple: because it's too big. The 300th term is 222232244629420445529739893461909967206666939096499764990979600, so you might imagine how big the 4000th is. You can't hold such value in a JavaScript variable. If you really want to calculate it, use an arbitrary precision library, and maybe something other than JavaScript if you want to calculate it fast. Check GNU Multiple Precision

Javascript Fibonacci

一世执手 提交于 2019-11-30 17:51:40
问题 function fibo() { var first,second,add; for(var i=0;i<4000;i++){ if(i === 0){ first = 1; second = 2; } add = first + second; first = second; second = add; } alert(add); } fibo(); not working shows infinity why? 回答1: Simple: because it's too big. The 300th term is 222232244629420445529739893461909967206666939096499764990979600, so you might imagine how big the 4000th is. You can't hold such value in a JavaScript variable. If you really want to calculate it, use an arbitrary precision library,

Can someone explain this lazy Fibonacci solution?

匆匆过客 提交于 2019-11-30 17:32:29
问题 This is the code: fibs = 0 : 1 : zipWith (+) fibs (drop 1 fibs) When evaluated, fibs is an infinite list of Fibonacci numbers. What I don't understand is how the list is concatenated. zipWith returns a list, so zipping fibs would yield this: 0 : 1 : [1] : [1,2] : [1,2,3] Because 0 : 1 : zipWith (+) [0,1] [1] yields [1] and zipWith (+) [0,1,1] [1,1] yields [1,2] etc). However, when I run the code, I get the correct result. What am I not understanding here? 回答1: Your "Because" is not telling