fibonacci

Nth Fibonacci number for n as big as 10^19?

大城市里の小女人 提交于 2019-12-18 12:02:23
问题 I am trying to make a program to find the nth Fibonacci number for 1 < n < 10^19. Here is my code using dynamic programming. memo = {} def fib(n): if n in memo: return memo[n] if n <= 2: f = 1 else: f = fib(n-1) + fib(n-2) memo[n]=f return f print fib(input()) % 1000000007 My code does not seem to work for large numbers. I get invalid response error. Any suggestions? 回答1: Getting the Nth fibonacci number when N is 10^19 is not goign to work if you do it the naive way (at least i would guess

Tail Recursion Fibonacci

情到浓时终转凉″ 提交于 2019-12-18 11:53:22
问题 How do I implement a recursive Fibonacci function with no loops running in O(n)? 回答1: Typically I'd be against posting an answer to a homework question like this, but everything posted so far seems to be overcomplicating things. As said in the comments above, you should just use recursion to solve the problem like you would do iteratively. Here's the iterative solution: def fib(n): a, b = 0, 1 while n > 0: a, b = b, a+b n -= 1 return a Here's an equivalent recursive solution: def fib(n): def

Fibonacci sequence in Javascript

巧了我就是萌 提交于 2019-12-18 04:51:00
问题 I am very new to programming in general and am having a hard time understanding this Fibonacci sequence example: var fib = [0, 1]; for (var i = 2; i < n; i++) { fib[ i ] = fib[ i - 1 ] + fib[ i - 2 ]; console.log(fib); } On the first iteration, index 2 is equal to 1, simple enough. But, when I try the second iteration with i = 3, I get: fib[ 3 ] = fib[ 3 - 1 ] + fib[ 3 - 2 ]; fib[ 3 ] = fib[ 2 ] + fib[ 1 ]; fib[ 3 ] = fib[ 3 ]; Where am I going wrong with my thinking? So far I have: var fib =

Fibonacci One-Liner

≡放荡痞女 提交于 2019-12-17 21:56:11
问题 I'm trying to solve questions from Project Euler in Ruby one-liners, and I'm curious if there's a more elegant solution for question two: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. Here is my one line solution in Ruby: (1..32)

Fibonacci in Scheme

删除回忆录丶 提交于 2019-12-17 21:30:08
问题 I am trying to understand recursion in Scheme and I have a hard time doing the dry run for it, for example a simple Fibonacci number problem. Could someone break down the steps in which the additions take place, for me? (define (fib n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib (- n 2))))) 回答1: If you're using Racket, as your tags indicate, then you have a built-in stepper. Enter the program into DrRacket, and click Step in the top-right menu: Then a stepper window will open up. Click Step over and

Fibonacci numbers generator in Swift 3

萝らか妹 提交于 2019-12-17 20:40:30
问题 The following Q&A covers a few methods of generating Fibonacci numbers in Swift, but it's quite outdated (Swift 1.2?): Sum of Fibonacci term using Functional Swift Question: How could we generate Fibonacci numbers neatly using modern Swift (Swift >= 3)? Preferably methods avoiding explicit recursion. 回答1: An alternative for Swift 3.0 would be to use the helper function public func sequence<T>(first: T, while condition: @escaping (T)-> Bool, next: @escaping (T) -> T) -> UnfoldSequence<T, T> {

Calculate the Fibonacci number (recursive approach) in compile time (constexpr) in C++11

戏子无情 提交于 2019-12-17 18:29:23
问题 I wrote the program Fibonacci number calculation in compile time (constexpr) problem using the template metaprogramming techniques supported in C++11. The purpose of this is to calculate the difference in the run-time between the template metaprogramming approach and the old conventional approach. // Template Metaprograming Approach template<int N> constexpr int fibonacci() {return fibonacci<N-1>() + fibonacci<N-2>(); } template<> constexpr int fibonacci<1>() { return 1; } template<>

Recursive Fib with Threads, Segmentation Fault?

心已入冬 提交于 2019-12-17 17:09:03
问题 Any ideas why it works fine for values like 0, 1, 2, 3, 4... and seg faults for values like >15? #include #include #include void *fib(void *fibToFind); main(){ pthread_t mainthread; long fibToFind = 15; long finalFib; pthread_create(&mainthread,NULL,fib,(void*) fibToFind); pthread_join(mainthread,(void*)&finalFib); printf("The number is: %d\n",finalFib); } void *fib(void *fibToFind){ long retval; long newFibToFind = ((long)fibToFind); long returnMinusOne; long returnMinustwo; pthread_t

How to find the nearest Fibonacci Series number?

被刻印的时光 ゝ 提交于 2019-12-17 14:55:28
问题 My next step is if the input is not in the Fibonacci Series, the program has to give an output with a number which is in the series that is nearest to the input. I do not know how to proceed, can anyone help me? def fibs(): a,b = 0,1 yield a yield b while True: a,b = b,a+b yield b n = int(input("please, enter a number: ")) for fib in fibs(): if n == fib: print("Yes! Your number is a Fibonacci number!") break if fib > n: print("No! Your number is not a Fibonacci number!") break 回答1: Why this

Why is my recursive function so slow in R?

一曲冷凌霜 提交于 2019-12-17 10:45:14
问题 The following takes about 30 seconds to run whereas I would expect it to be nearly instant. Is there a problem with my code? x <- fibonacci(35); fibonacci <- function(seq) { if (seq == 1) return(1); if (seq == 2) return(2); return (fibonacci(seq - 1) + fibonacci(seq - 2)); } 回答1: Patrick Burns gives an example in R Inferno of one way to do memoization in R with local() and <<- . In fact, it's a fibonacci: fibonacci <- local({ memo <- c(1, 1, rep(NA, 100)) f <- function(x) { if(x == 0) return