From 305ac2de29d110e3ccf526a6304b037f17860fcf Mon Sep 17 00:00:00 2001 From: Ardi Madadi Date: Sat, 10 Dec 2022 14:08:08 +1300 Subject: Ardi's entry in Java --- entries/ardier/java/Main.java | 23 +++++++++++++++++++++++ people.json | 5 +++++ 2 files changed, 28 insertions(+) create mode 100644 entries/ardier/java/Main.java 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 BASE_CASE; + + public static void main(String[] args) { + BASE_CASE = new HashSet(); + 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 diff --git a/people.json b/people.json index f7f7ae7..897a585 100644 --- a/people.json +++ b/people.json @@ -714,7 +714,12 @@ { "name": "One-liners in Python", "link": "./entries/ardier/python/main.py" + }, + { + "name": "One-liners in Java", + "link": "./entries/ardier/java/Main.java" } + ] } ] -- cgit v1.2.3-70-g09d2