Skip to content

Commit

Permalink
feat: fibonacci using dynamic programming (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian-crypto authored Nov 10, 2024
1 parent 8ea5c8c commit 2205c4f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,24 @@ public class FibonacciSnippet {
}
}

/**
* Fibonacci series using dynamic programming. Works for larger ns as well.
*
* @param n given number
* @return fibonacci number for given n
*/
public static int fibonacciBig(int n) {
int previous = 0;
int current = 1;
for (int i = 0; i < n - 1; i++) {
int t = previous + current;
previous = current;
current = t;
}

return current;
}

/**
* Example of what an iterative implementation of Fibonacci looks like.
*
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/math/FibonacciSnippet.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
* Copyright (c) 2017-2024 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -46,6 +46,24 @@ public static int fibonacci(int n) {
}
}

/**
* Fibonacci series using dynamic programming. Works for larger ns as well.
*
* @param n given number
* @return fibonacci number for given n
*/
public static int fibonacciBig(int n) {
int previous = 0;
int current = 1;
for (int i = 0; i < n - 1; i++) {
int t = previous + current;
previous = current;
current = t;
}

return current;
}

/**
* Example of what an iterative implementation of Fibonacci looks like.
*
Expand Down
22 changes: 21 additions & 1 deletion src/test/java/math/FibonacciSnippetTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2017-2022 Ilkka Seppälä
* Copyright (c) 2017-2024 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -51,6 +51,26 @@ void testFibonacci() {
assertEquals(55, FibonacciSnippet.fibonacci(10));
}

/**
* Tests for {@link FibonacciSnippet#fibonacciBig(int)}.
*/
@Test
void testFibonacciBig() {
assertEquals(0, FibonacciSnippet.fibonacci(0));
assertEquals(1, FibonacciSnippet.fibonacci(1));
assertEquals(1, FibonacciSnippet.fibonacci(2));
assertEquals(2, FibonacciSnippet.fibonacci(3));
assertEquals(3, FibonacciSnippet.fibonacci(4));
assertEquals(5, FibonacciSnippet.fibonacci(5));
assertEquals(8, FibonacciSnippet.fibonacci(6));
assertEquals(13, FibonacciSnippet.fibonacci(7));
assertEquals(21, FibonacciSnippet.fibonacci(8));
assertEquals(34, FibonacciSnippet.fibonacci(9));
assertEquals(55, FibonacciSnippet.fibonacci(10));
assertEquals(6765, FibonacciSnippet.fibonacci(20));
assertEquals(102334155, FibonacciSnippet.fibonacci(40));
}

/**
* Tests for {@link FibonacciSnippet#iterativeFibonacci(int)}.
*/
Expand Down

0 comments on commit 2205c4f

Please sign in to comment.