Created
January 22, 2017 06:32
-
-
Save pierceh89/8f329434f15f1609d5b772cc1c353deb to your computer and use it in GitHub Desktop.
Fibonaci number DP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
void compute_fibonaci(int* container, int size) { | |
container[0] = 0; | |
container[1] = 1; | |
for(int i=2; i < size; i++) { | |
container[i] = container[i-1] + container[i-2]; | |
} | |
} | |
void print_fibonaci(int* container, int size) { | |
for(int i=0; i < size; i++) { | |
printf("fibonaci %d = %d\n", i, container[i]); | |
} | |
} | |
int main(int argc, char* argv[]) { | |
int* memoization; | |
int n; | |
scanf("%d", &n); | |
memoization = (int *)malloc(sizeof(int) * n); | |
compute_fibonaci(memoization, n); | |
print_fibonaci(memoization, n); | |
free(memoization); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Fibonacci { | |
int[] memoization; | |
int size; | |
public Fibonacci(int n) { | |
size = n; | |
memoization = new int[size]; | |
computeFibonacci(); | |
} | |
private void computeFibonacci() { | |
memoization[0] = 0; | |
memoization[1] = 1; | |
for(int i=2; i < size; i++) { | |
memoization[i] = memoization[i-1] + memoization[i-2]; | |
} | |
} | |
public void printFibonacci() { | |
for(int i=0; i < size; i++) { | |
System.out.printf("fibonacci %d = %d\n", i, memoization[i]); | |
} | |
} | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int size = scanner.nextInt(); | |
Fibonacci fibonacci = new Fibonacci(size); | |
fibonacci.printFibonacci(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compute_fibonacci(size): | |
memoization = [0, 1] | |
for i in range(2, size): | |
memoization.append(memoization[i-1] +memoization[i-2]) | |
return memoization | |
def print_fibonacci(fibonacci_numbers): | |
size = len(fibonacci_numbers) | |
for i in range(size): | |
print("fibonacci " + str(i) + " = " + str(fibonacci_numbers[i])) | |
if __name__ == '__main__': | |
size = raw_input() | |
fibonacci_results = compute_fibonacci(int(size)) | |
print_fibonacci(fibonacci_results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment