fibonacci

Dynamic Programming - Fibonacci

断了今生、忘了曾经 提交于 2021-02-19 04:59:05
问题 So basically, I am a learning programmer and this week I was introduced to dynamic programming. Our task was to find the Fibonacci sequence using dynamic programming. This pseudo code was supplied which would obviously be in a function: init table to 0s if n ≤ 1 return n else if table[n-1] = 0 table[n-1] = dpFib(n-1) if table[n-2] = 0 table[n-2] = dpFib(n-2) table[n] = table[n-1] + table[n-2] return table[n] The majority of this was simple to change to code but I'm not sure how to initialise

Recursion, memoization and mutable default arguments in Python

人走茶凉 提交于 2021-02-19 02:48:52
问题 "Base" meaning without just using lru_cache. All of these are "fast enough" -- I'm not looking for the fastest algorithm -- but the timings surprised me so I was hoping I could learn something about how Python "works". Simple loop (/tail recursion): def fibonacci(n): a, b = 0, 1 if n in (a, b): return n for _ in range(n - 1): a, b = b, a + b return b Simple memoized: def fibonacci(n, memo={0:0, 1:1}): if len(memo) <= n: memo[n] = fibonacci(n - 1) + fibonacci(n - 2) return memo[n] Using a

Non-recursive Fibonacci Sequence in Assembly

梦想与她 提交于 2021-02-11 15:45:04
问题 In some homework, I have to create a Fibonacci Sequence program in Assembly. I created this code, but it doesn't seem to be working correctly and I am not sure as to why. I believe that I am doing this correctly, but EAX remains "2" every loop. INCLUDE Irvine32.inc .data prev DWORD ? next DWORD ? val DWORD ? count DWORD ? total DWORD ? myMsg BYTE "Fibonacci Sequence ",0dh,0ah,0 .code main PROC mov ecx,15 mov val,1 mov prev,-1 mov eax,1 mov edx,OFFSET myMsg call WriteString L1: mov count,ecx

Fibonacci Recursion Value tracer

隐身守侯 提交于 2021-02-11 07:55:15
问题 So I need to write a program which uses a recursive function to store the value of input arguments in the order they were made. e.g. If my function is [f trace]=fibo_trace(6,[]) it should return [f trace]=fibo_trace(6,[]) f= 8 trace= 6 4 2 3 1 2 5 3 1 2 4 2 3 1 2 With trace being the values with which the recursive call is being initialized and f being the 6th element in the fibonacci series. Here is my code function [f,trace] = fibo_trace(n,v) persistent ptrace; % must specify persistent v=n

Fibonacci series in C++

限于喜欢 提交于 2021-02-07 06:56:43
问题 #include <iostream> using namespace std; int main() { int num1 = 0; int num2 = 1; int num_temp; int num_next = 1; int n; cin >> n; for (int i = 0; i < n; i++){ cout << num_next << " "; num_next = num1 + num2; num1 = num2; num_temp = num2; num2 = num_next - num1; num1 = num_temp; } return 0; } I have to output the first "n" fibonacci numbers however I think there is some problem in logic.. I can't find out what am I doing wrong. The first 3 or 4 elements are correct but then a problem occurs..

Fibonacci series in C++

杀马特。学长 韩版系。学妹 提交于 2021-02-07 06:56:20
问题 #include <iostream> using namespace std; int main() { int num1 = 0; int num2 = 1; int num_temp; int num_next = 1; int n; cin >> n; for (int i = 0; i < n; i++){ cout << num_next << " "; num_next = num1 + num2; num1 = num2; num_temp = num2; num2 = num_next - num1; num1 = num_temp; } return 0; } I have to output the first "n" fibonacci numbers however I think there is some problem in logic.. I can't find out what am I doing wrong. The first 3 or 4 elements are correct but then a problem occurs..

Sum of Even Fibonacci Numbers < X

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-05 06:14:43
问题 I'm working on this one and I seem to have a working solution but I have difficulty understanding its behaviour. Here is what I have. #!/usr/bin/python def even_fib_sums(limit): number = 1 last = 0 before_last = 0 total = 0 for counter in range (0,limit): before_last = last last = number number = before_last + last if not number % 2: total += number yield total print sum(even_fib_sums(4000000)) I'm new to programming but it makes sense to me that this is not very effective considering I need

For a given value of n and m, find fib(n) mod m where n is very huge. (Pisano Period)

故事扮演 提交于 2021-02-04 16:43:35
问题 Input Integers 'n' (up to 10^14) and 'm'(up to 10^3) Output Fib(n) modulo m Sample Cases Input: 239 1000 Output: 161 Input: 2816213588 239 Output: 151 Hint given in Question As it is not possible to iterate 'n' times (because n is huge), consider using Pisano Period(repetition of remainders when every element Fibonacci series is divided by any integer) Code which I wrote (maybe wrong, but passes above-mentioned cases) n, m = map(int, input().split()) a, b = 0, 1 fib_rems = [0, 1] # remainders

Finding the fibonacci number of large number

早过忘川 提交于 2021-01-28 18:24:33
问题 I wrote the following program for finding the modulus of large Fibonacci's number. This can solve large numbers but fails to compute in cases like fibo_dynamic(509618737,460201239,229176339) where a = 509618737 , b = 460201239 and N = 229176339 . Please help me to make this work. long long fibo_dynamic(long long x,long long y,long long n, long long a[]){ if(a[n]!=-1){ return a[n]; }else{ if(n==0){ a[n]=x; return x; }else if(n==1){ a[n]=y; return y; }else { a[n]=fibo_dynamic(x,y,n-1,a)+fibo

Smth about Binet formula

血红的双手。 提交于 2021-01-27 19:41:22
问题 Why does the Binet formula( O(LogN), but it is not exactly ) work worse in time than the iteration method( O(n) )? static double SQRT5 = Math.Sqrt(5); static double PHI = (SQRT5 + 1) / 2; public static int Bine(int n) { return (int)(Math.Pow(PHI, n) / SQRT5 + 0.5); } static long[] NumbersFibonacci = new long[35]; public static void Iteracii(int n) { NumbersFibonacci[0] = 0; NumbersFibonacci[1] = 1; for (int i = 1; i < n - 1; i++) { NumbersFibonacci[i + 1] = NumbersFibonacci[i] +