Skip to content

Instantly share code, notes, and snippets.

@artem-smotrakov
Created March 14, 2020 15:16
Show Gist options
  • Save artem-smotrakov/f00eed8b260ece17e4c1f0779127cb09 to your computer and use it in GitHub Desktop.
Save artem-smotrakov/f00eed8b260ece17e4c1f0779127cb09 to your computer and use it in GitHub Desktop.
Calculating Tribonacci numbers with new switch expressions in Java 14
public class Tribonacci {
public static void main(String[] args) {
for (String arg : args) {
int n = Integer.valueOf(arg);
System.out.printf("Tribonacci(%d) = %d%n", n, tribonacci(n));
}
}
static int tribonacci(int n) {
return switch (n) {
case 0 -> 0;
case 1, 2 -> 1;
default -> tribonacci(n - 1) + tribonacci(n - 2) + tribonacci(n - 3);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment