fibonacci

Optimization of Fibonacci sequence generating algorithm

点点圈 提交于 2019-12-06 14:36:15
As we all know, the simplest algorithm to generate Fibonacci sequence is as follows: if(n<=0) return 0; else if(n==1) return 1; f(n) = f(n-1) + f(n-2); But this algorithm has some repetitive calculation. For example, if you calculate f(5), it will calculate f(4) and f(3). When you calculate f(4), it will again calculate both f(3) and f(2). Could someone give me a more time-efficient recursive algorithm? One simple way is to calculate it iteratively instead of recursively. This will calculate F(n) in linear time. def fib(n): a,b = 0,1 for i in range(n): a,b = a+b,a return a Hynek -Pichi-

Optimal Huffman Code for Fibonacci numbers

北慕城南 提交于 2019-12-06 14:36:06
What is an optimal Huffman code for the following characters whose frequencies are the first 8 Fibonacci numbers: a : 1, b : 1, c : 2, d : 3, e : 5, f : 8, g : 13, h : 21? Generalize the case to find an optimal code when the frequencies are the first n Fibonacci numbers. This is one of the assignment problems I have. I'm not asking for a straight answer, just for some resources. Where should I look to put the pieces together to answer the questions? Read - http://en.wikipedia.org/wiki/Huffman_coding - In particular, pay attention to the phrase "A binary tree is generated from left to right

My Fibonacci sequence as a recursive function is an infinite loop

六月ゝ 毕业季﹏ 提交于 2019-12-06 13:41:34
问题 The following function recurses infinitely and I don't see why. It enters the conditional statements but doesn't seem to be terminating with the return statement. use strict; use warnings; print fibonacci(100); sub fibonacci { my $number = shift; if ($number == 0) { print "return 0\n"; return 0; } elsif ($number == 1) { print "return 1\n"; return 1; } else { return fibonacci($number-1) + fibonacci($number-2); } } 回答1: Your loop does not recurse infinitely, it just takes way too long with an

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

不打扰是莪最后的温柔 提交于 2019-12-06 06:17:46
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] Try this, a recursive implementation that returns a list of numbers by first calculating the list of previous values: def fib(n): if n == 0: return [0] elif n == 1: return [0, 1] else: lst = fib(n-1

Fibonacci sequence backward

我与影子孤独终老i 提交于 2019-12-06 04:06:05
问题 Here is the code: class Fibonacci { static final int MIN_INDEX = 1; public static void main (String[] args){ int high = 1; int low = 1; String jel; System.out.println("9: " + high); for (int i = 8; i >= MIN_INDEX; i--){ if (high % 2 == 0) jel = " *"; else jel = " "; System.out.println(i + ": " + high + jel); high = low + high; low = high - low; } } } I want to make this program, to write the output numbers backward. So I want that not only the ' i ' step from the last to the first, but the

Recursive Fibonacci using Fork (in C)

一笑奈何 提交于 2019-12-06 01:54:24
问题 I'm attempting to write a function that recursively computes the resulting fibonacci number from a given int n using forks in C. Here is the function specification: If print is true, print it. Otherwise, provide it to the parent process. The solution should be recursive and it must fork a new child for each call. Each process should call doFib() exactly once. The method signature cannot be changed. Helper functions cannot be used. Here is what I've written thus far based on my understanding

Replacing recursion with while loop (stair climbing puzzle): Python

天大地大妈咪最大 提交于 2019-12-05 21:57:36
I'm practicing replacing recursion with while loops, and I'm stuck on the following problem. How many ways can you go up a staircase of length n if you can only take the stairs 1 or 2 at a time? The recursive solution is pretty simple: def stairs(n): if n <= 1: return 1 else: return stairs(n-2) + stairs(n-1) I feel like the structure for the iterative program should go something like this: def stairs_iterative(n): ways = 0 while n > 1: # do something ways +=1 return ways But I don't know what I need to put in the #do something part. Can someone help me? Pseudocode is fine! This amounts to the

Haskell: Improving my tail-recursive fibonacci implementation

好久不见. 提交于 2019-12-05 20:03:49
I have come up with the following tail-recursive fibonacci generator that works: let { fibo :: Integral x => [x]->x->x->x->[x] fibo l x y 0 = l fibo l x y n = fibo (l ++ [y+x] ++ [y+x+y]) (x+y) (y+x+y) (n-1) } Pardon me for the whole implementation put in one line because i am using the GHCi and haven't quite learnt how to put this in a file and run (i am yet to reach there). What i want to know is how this call: fibo [0, 1] 0 1 5 can be improved. I do not want to pass the initial list with 0 and 1 and then pass 0 and 1 again with the limit. I believe that the implementation can be changed.

Prolog; try to make fibonacci more effective?

别等时光非礼了梦想. 提交于 2019-12-05 19:05:01
问题 This logic programming is really making a lap dance on my imperative programming skills. This is homework, so please just don't drop me the answer. This is what I have: fibo(N,1) :- N < 2, !. fibo(N,R) :- N1 is N-1, N2 is N-2, fibo(N1,R1), fibo(N2,R2), R is R1+R2. I'm suppose to make another function that looks like this; fib(N,Value,LastValue) . N is the n'th number, and value is the return value. I don't understand how I can rewrite this using accumulation. And since it counts backwards I

NumberFormatException: Infinite or NaN

此生再无相见时 提交于 2019-12-05 16:16:55
I have a method that takes n and returns nth Fibonacci number. Inside the method implementation I use BigDecimal to get the nth Fibonacci number then I use method toBigInteger() to get the number as a BigInteger object and that's surely because I am working with huge numbers in my application. I keep getting correct results until I pass 1475 as an argument for my method. I get NumberFormatException: Infinite or NaN in this case without any clear reason for me. Could you please explain me why am I getting this exception? Here's my method: BigInteger getFib(int n){ double phi = (1 + Math.sqrt(5)