fibonacci

Ruby Fibonacci algorithm

最后都变了- 提交于 2019-11-27 09:08:38
The following is a method I wrote to calculate a value in the Fibonacci sequence: def fib(n) if n == 0 return 0 end if n == 1 return 1 end if n >= 2 return fib(n-1) + (fib(n-2)) end end It works uptil n = 14, but after that I get a message saying the program is taking too long to respond (I'm using repl.it). Anyone know why this is happening? Naive fibonacci makes a lot of repeat calculations - in fib(14) fib(4) is calculated many times. You can add memoization to your algorithm to make it a lot faster: def fib(n, memo = {}) if n == 0 || n == 1 return n end memo[n] ||= fib(n-1, memo) + fib(n-2

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

旧城冷巷雨未停 提交于 2019-11-27 08:58:29
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? for me the simplest defines a recursive inner tail function: def fib: Stream[Long] = { def tail(h: Long, n: Long): Stream[Long] = h #:: tail(n, h + n) tail(0, 1) } This

Step by Step explanation of this code [closed]

南楼画角 提交于 2019-11-27 08:54:12
问题 def mystery(n): a, b = 0, 1 while a < n: print (a) a, b = b, a + b If someone could give me a line by line explanation of this code, as also inform of why it will not run, and what extra code needs to be added. 回答1: def mystery(n): # define a function named "mystery", that takes one argument called "n" a, b = 0, 1 # make a variable named "a" and set it to 0; make a variable named "b" and set it to 1 while a < n: # as long as "a" is smaller than "n" print (a) # display the value contained in

Fibonacci Sequence in C generating negatives?

混江龙づ霸主 提交于 2019-11-27 08:12:10
问题 I'm new to programming and need help in C. I am writing a program to generate a Fibonacci sequence for values with up to 1000 digits. Here is my code: #include <stdio.h> int main(void) { int seq[1000]; int i,n; printf("How many Fibonacci numbers do you want?: "); scanf("%d",&n); seq[0] = 0; seq[1] = 1; for(i = 2; i < n; i++) seq[i] = seq[i-1] + seq[i-2]; for (i = 1; i < n; i++) printf("%d: %d\n", i, seq[i]); return 0; } Now the problem is, the numbers are all correct up until the 47th number.

Python - Difference between variable value declaration on Fibonacci function

南笙酒味 提交于 2019-11-27 08:11:53
问题 I'm kind of beginner in python. I was looking at one the types to make a fibonacci function, def fib(n): a=0 b=1 while a<n: print a a,b=b,a+b and I saw the a,b=b,a+b declaration. So, I thought a=b and b=a+b were the same to a,b=a,b+a, so I changed the function for it to be like this: def fib(n): a=0 b=1 while a<n: print a a=b b=a+b and I thought it would be right, but when I executed the program, I got a different output. Can someone explain to me the difference between those two types of

Fibonacci sequence in Ruby (recursion)

扶醉桌前 提交于 2019-11-27 07:19:32
I'm trying to implement the following function, but it keeps giving me the stack level too deep (SystemStackError) error. Any ideas what the problem might be ? def fibonacci( n ) [ n ] if ( 0..1 ).include? n ( fibonacci( n - 1 ) + fibonacci( n - 2 ) ) if n > 1 end puts fibonacci( 5 ) Pritesh Jain Try this def fibonacci( n ) return n if ( 0..1 ).include? n ( fibonacci( n - 1 ) + fibonacci( n - 2 ) ) end puts fibonacci( 5 ) # => 5 check this post too Fibonacci One-Liner and more .. https://web.archive.org/web/20120427224512/http://en.literateprograms.org/Fibonacci_numbers_(Ruby) You have now

Recursive Fibonacci memoization

一世执手 提交于 2019-11-27 07:08:32
I need some help with a program I'm writing for my Programming II class at universtiy. The question asks that one calculates the Fibonacci sequence using recursion. One must store the calculated Fibonacci numbers in an array to stop unnecessary repeated calculations and to cut down to the calculation time. I managed to get the program working without the array and memorization, now I'm trying to implement that and I'm stuck. I'm not sure how to structure it. I've Googled and skimmed through some books but haven't found much to help me solve how to implement a solution. import javax.swing

How to fix my Fibonacci stream in Scala

余生长醉 提交于 2019-11-27 06:50:36
问题 I defined a function to return Fibonacci stream as follows: def fib:Stream[Int] = { Stream.cons(1, Stream.cons(2, (fib zip fib.tail) map {case (x, y) => println("%s + %s".format(x, y)); x + y})) } The functions work ok but it looks inefficient (see the output below) scala> fib take 5 foreach println 1 2 1 + 2 3 1 + 2 2 + 3 5 1 + 2 1 + 2 2 + 3 3 + 5 8 So, it looks like the function calculates the n-th fibonacci number from the very beginning. Is it correct? How would you fix it? 回答1: That is

An inverse Fibonacci algorithm?

六月ゝ 毕业季﹏ 提交于 2019-11-27 06:15:53
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 way of doing this any faster than that? EDIT: currently, the best solution posted here runs in O(log n)

Test if a number is fibonacci

冷暖自知 提交于 2019-11-27 05:59:30
I know how to make the list of the Fibonacci numbers, but i don't know how can i test if a given number belongs to the fibonacci list - one way that comes in mind is generate the list of fib. numbers up to that number and see if it belongs to the array, but there's got to be another, simpler and faster method. Any ideas ? Il-Bhima A very nice test is that N is a Fibonacci number if and only if 5 N^2 + 4 or 5N^2 – 4 is a square number. For ideas on how to efficiently test that a number is square refer to the SO discussion . Hope this helps JRL A positive integer ω is a Fibonacci number if and