Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added improved Fibonacci sequence number finder with dynamic programming #223

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading