Created
March 14, 2020 15:16
-
-
Save artem-smotrakov/f00eed8b260ece17e4c1f0779127cb09 to your computer and use it in GitHub Desktop.
Calculating Tribonacci numbers with new switch expressions in Java 14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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