Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 070a532

Browse files
committed
FEAT Implemented recursive method that computes fibonacci sum for an integer
1 parent 4e0eb0a commit 070a532

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎src/PracticeAlgorithms.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import java.util.Random;
22

3+
/**
4+
* class containing static methods to compute the fibonacci sequence sum for an integer
5+
* the class contains two implementations:
6+
* 1. a recursive method
7+
* 2. a much faster method utilizing memoization and dynamic programming
8+
* */
9+
class Fibonacci {
10+
// recursive implementation
11+
public static long Fib(int N) {
12+
if (N == 0) return 0;
13+
if (N == 1) return 1;
14+
15+
return Fib(N - 2) + Fib(N - 1);
16+
}
17+
}
18+
319
public class PracticeAlgorithms {
420
public static boolean isBetweenZeroAndOne(double x, double y) {
521
return (0 < x && x < 1) && (0 < y && y < 1);
@@ -119,5 +135,11 @@ public static void main(String[] args) {
119135
System.out.println();
120136

121137
System.out.println(lg(32));
138+
System.out.println();
139+
140+
// loop utilizing the recursive fib method
141+
for (int i = 0; i < 101; i++) {
142+
System.out.println(Fibonacci.Fib(i));
143+
}
122144
}
123145
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /