fibonacci

Beginning Python fibonacci generator

有些话、适合烂在心里 提交于 2019-12-08 07:31:06
问题 I'm trying to make a fibonacci number generator that stops at a given amount, but it usually goes past the amount. What am I doing wrong? #Fibonacci number generator a=0 b=1 print("Fibonacci number generator.") stopNumber=input("How high do you want to go? If you want to go forever, put n.") print(1) while stopNumber=="n": a=a+b b=b+a print(a) print(b) else: while int(stopNumber) > a or int(stopNumber) > b: a=a+b b=b+a print(a) print(b) 回答1: Using your code: #Fibonacci number generator a=0 b

Optimal Huffman Code for Fibonacci numbers

﹥>﹥吖頭↗ 提交于 2019-12-08 06:38:32
问题 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? 回答1: Read - http://en.wikipedia.org/wiki

How can I get Fibonacci(n) in an efficient way with Scala Actor?

独自空忆成欢 提交于 2019-12-08 04:34:36
问题 The algorithm is just like this. def fib(x: Int): BigInt = { x match { case 1 => BigInt(1) case 2 => BigInt(1) case x => fib(x-1) + fib(x-2) } } I try to make the algorithm parallel with Actor in Scala. But my code is extremely slow compare with the one without Actor! Is there a good way to make it work? 回答1: For not large size of n , the serial code will always be faster (Much much faster in cases of tail recursion). This is because calling a new function will be faster than starting a new

Lazy fibonacci in Ruby

泄露秘密 提交于 2019-12-08 00:08:55
问题 I can write a lazy fibonacci in Clojure like this: (def fib (lazy-cat [1 1] (map +' fib (rest fib)))) and I'm trying (unsuccessfully) to write it in Ruby like this: fib = Enumerator.new do |yielder| yielder << 1 << 1 fib.zip(fib.drop(1)).map do |a,b| yielder << (a + b) end end In the simplified case, this works: fib = Enumerator.new do |yielder| yielder << 1 << 1 puts "here" end puts fib.take(2).inspect puts fib.drop(1).take(1).inspect but this doesn't: fib = Enumerator.new do |yielder|

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

淺唱寂寞╮ 提交于 2019-12-07 16:33:00
问题 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

Haskell: Improving my tail-recursive fibonacci implementation

女生的网名这么多〃 提交于 2019-12-07 14:57:16
问题 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

Index of a really big Fibonacci Number

别等时光非礼了梦想. 提交于 2019-12-07 08:15:28
问题 I need to calculate the index of a Fibonacci number with JavaScript, within the Fibonacci sequence. I need to do this without using recursion, or a loop. I found the following formula in the Math forum: n=⌊logφ(F⋅5√+12)⌋ and coded it in JavaScript: function fibIndex(fib) { fib = BigNumber(fib); return logBasePhi(fib.times(Math.sqrt(5)).plus((1/2))); } function phi() { return (1 + Math.sqrt(5))/ 2; } function getBaseLog(x, y) { return Math.log(y) / Math.log(x); } function logBasePhi(x) {

Regarding the Fibonacci Sequence example in Python's function tutorial

做~自己de王妃 提交于 2019-12-07 06:30:46
问题 This is what they have: def fib(n): a, b = 0, 1 while a < n: print a, a, b = b, a+b This is what I have: def fib(n): a = 0 b = 1 while a < n: print a a = b b = b+a The first returns the correct sequence when employed, whereas mine proceeds 0, 1, 2, 4, 8, 16, 32... I am currently learning programming (no prior computer science education), and it's clear that the problem is how I've defined my variables. What is the difference between separating the variables by commas and separating variables

Calculating a huge fibonacci number modulo m in Python

♀尐吖头ヾ 提交于 2019-12-07 03:25:26
The goal of this problem is to calculate F[n] mod m . Here the inputs are n and m , where n stands for the index of the fibonacci number, say F[0] = 0, F[1] = 1, F[2] = 1, F[3]= 2 and m stands for the number by which F[n] will be divided. The constraints are: n >= 1 and n <= 10^18 m >= 2 and m <= 10^5 I have gone this problem so far and been able to generate the exact output of this problem except when I give 100000 as the value of m , it exceeds the time limit. The time limit is 5 seconds. If the value of m is given between and including from 2 to 99999, my program generates the correct

Recursive thread creation in python

落爺英雄遲暮 提交于 2019-12-06 21:55:38
I'm trying to implement a recursive Fibonacci series which returns the value at an index. It's a homework and needs to be done using multi-threading. This is what I've done so far. My question is how do I add the results from live_thread1 and live_thread2 . The threads have to be created at every level in the recursion. def Recursive(n): if n< 2: return n else: return Recursive(n- 1) + Recursive(n- 2) def FibonacciThreads(n): if n< 2: return n else: thread1 = threading.Thread(target=FibonacciThreads,args=(n-1,)) thread2 = threading.Thread(target=FibonacciThreads,args=(n-2,)) thread1.start()