fibonacci

Is it possible to generate 40,000+ element of Fibonacci recursively in Lisp?

馋奶兔 提交于 2019-12-09 20:44:57
问题 I'm trying to solve Project Euler question 2 with Lisp. This recursive solution blows the stack on execution, but I thought Lisp (using clisp) would recognize the tail recursion. This is being entered into the top-level. (defun e2-problem (&optional (f1 1) (f2 1) (sum 0)) "Sum fibonacci sequence, even terms up to 4 million" (if (> f2 4000000) sum) (e2-problem f2 (+ f1 f2) (if (evenp f2) (+ sum f2) sum)) Is my implementation not correctly arranged for optimization? I imagine this would hinder

Non Brute Force Solution to Project Euler 25

眉间皱痕 提交于 2019-12-09 16:41:16
问题 Project Euler problem 25: The Fibonacci sequence is defined by the recurrence relation: F n = F n−1 + F n−2 , where F 1 = 1 and F 2 = 1. Hence the first 12 terms will be F 1 = 1, F 2 = 1, F 3 = 2, F 4 = 3, F 5 = 5, F 6 = 8, F 7 = 13, F 8 = 21, F 9 = 34, F 10 = 55, F 11 = 89, F 12 = 144 The 12th term, F 12 , is the first term to contain three digits. What is the first term in the Fibonacci sequence to contain 1000 digits? I made a brute force solution in Python, but it takes absolutely forever

Infinite fibonacci sequence

僤鯓⒐⒋嵵緔 提交于 2019-12-09 16:17:57
问题 I'm trying to imitate Haskell's famous infinite fibonacci list in F# using sequences. Why doesn't the following sequence evaluate as expected? How is it being evaluated? let rec fibs = lazy (Seq.append (Seq.ofList [0;1]) ((Seq.map2 (+) (fibs.Force()) (Seq.skip 1 (fibs.Force()))))) 回答1: The problem is that your code still isn't lazy enough: the arguments to Seq.append are evaluated before the result can be accessed, but evaluating the second argument ( Seq.map2 ... ) requires evaluating its

Implement fibonacci in Clojure using map/reduce

徘徊边缘 提交于 2019-12-09 14:16:45
问题 Is it possible to implement the fibonacci series in Clojure efficiently using reduce ? What would the "accumulator" contain? I imagine that it will have to be lazy. It's obvious how to do it using recursion or loop/recur. 回答1: You could use a pair of successive fibonacci values as the accumulator, as follows: (reduce (fn [[a b] _] [b (+ a b)]) ; function to calculate the next pair of values [0 1] ; initial pair of fibonnaci numbers (range 10)) ; a seq to specify how many iterations you want =

Fast Fibonacci computation

泪湿孤枕 提交于 2019-12-09 06:46:16
问题 I saw a comment on Google+ a few weeks ago in which someone demonstrated a straight-forward computation of Fibonacci numbers which was not based on recursion and didn't use memoization. He effectively just remembered the last 2 numbers and kept adding them. This is an O(n) algorithm, but he implemented it very cleanly. So I quickly pointed out that a quicker way is to take advantage of the fact that they can be computed as powers of [[0,1],[1,1]] matrix and it requires only a O(log(N))

Determining the individual letters of Fibonacci strings?

我怕爱的太早我们不能终老 提交于 2019-12-09 04:29:03
问题 The Fibonacci strings are defined as follows: The first Fibonacci string is "a" The second Fibonacci string is "bc" The (n + 2)nd Fibonacci string is the concatenation of the two previous Fibonacci strings. For example, the first few Fibonacci strings are a bc abc bcabc abcbcabc The goal is, given a row and an offset, to determine what character is at that offset. More formally: Input: Two integers separated by a space - K and P(0 < K ≤ 10 9 ), ( < P ≤ 10 9 ), where K is the line number of

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

风格不统一 提交于 2019-12-08 14:25:29
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? 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 actor. Plus there will contention among threads and context switches. In the below code, I start a new actor

printing fibonacci series using recursion in c++ [closed]

ぃ、小莉子 提交于 2019-12-08 13:54:37
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I just started learning c++ by myself. I am trying to practice recusion now. I want to print all n(input by user) fibonacci numbers using recursion, but it does not work. Could you help me? Thank you!! #include <iostream> using namespace std; int fibonacci(int n) { if (n==1) { return 1; cout<<1<<" "; } else if

Time Complexity of Fibonacci Algorithm [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-08 08:28:40
问题 This question already has answers here : Computational complexity of Fibonacci Sequence (11 answers) Closed 3 years ago . So, i've got a recursive method in Java for getting the 'n'th fibonacci number - The only question i have, is: what's the time complexity? I think it's O(2^n), but i may be mistaken? (I know that iterative is way better, but it's an exercise) public int fibonacciRecursive(int n) { if(n == 1 || n == 2) return 1; else return fibonacciRecursive(n-2) + fibonacciRecursive(n-1);

Recursive thread creation in python

感情迁移 提交于 2019-12-08 07:45:00
问题 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