#Java - 2 haikus
protected static
int factorial(int n) {
if (n == 0) {
return n + 1;
} return factorial(n
- 1) * n;}
Even when the question isn't code-golf, I often catch myself golfing the answer. In this case, I golfed the number of haikus.
I pronounce it so:
protected static
int factorial int n
if n is zero
return n plus one
return factorial n
minus one times n
Test program:
class Factorial {
public static void main(String[]
command_line_run_args) {
int i = 0;
while (0 == 0)
System.out.
println(i + "!"
+ " has a value of " +
factorial(i++));}
protected static
int factorial(int n) {
if (n == 0) {
return n + 1;
} return factorial(n
- 1) * n;}}
Note that this program starts outputting 0
s fast; that is a result of overflow. You could easily get larger correct numbers by changing each int
to long
.