aboutsummaryrefslogtreecommitdiff
path: root/entries/ardier/java
diff options
context:
space:
mode:
authorArdi Madadi2022-12-10 01:08:08 +0000
committerArdi Madadi2022-12-10 01:08:08 +0000
commit305ac2de29d110e3ccf526a6304b037f17860fcf (patch)
tree092e35c364d134f92404f229c5f54e0dcffd9037 /entries/ardier/java
parent401c8df449eecce4df1ba459d163d20b2caf6910 (diff)
Ardi's entry in Java
Diffstat (limited to 'entries/ardier/java')
-rw-r--r--entries/ardier/java/Main.java23
1 files changed, 23 insertions, 0 deletions
diff --git a/entries/ardier/java/Main.java b/entries/ardier/java/Main.java
new file mode 100644
index 0000000..554e346
--- /dev/null
+++ b/entries/ardier/java/Main.java
@@ -0,0 +1,23 @@
+import java.util.HashSet;
+
+public class Main {
+ private static HashSet<Integer> BASE_CASE;
+
+ public static void main(String[] args) {
+ BASE_CASE = new HashSet<Integer>();
+ BASE_CASE.add(1);
+ BASE_CASE.add(2);
+
+ for (int i = -20; i < 20; i++) {
+ System.out.println("The fib value for " + i + " is " + fib(i));
+ }
+ }
+
+ private static int fib(int entry) {
+ return entry == 0 ? 0 : ((entry < 0 && entry % 2 == 0) ? -fibHelper(Math.abs(entry)) : fibHelper(Math.abs(entry)));
+ }
+
+ private static int fibHelper(int entry) {
+ return BASE_CASE.contains(entry) ? 1 : fibHelper(entry - 1) + fibHelper(entry - 2);
+ }
+} \ No newline at end of file