Tag Archives: Buzz

Recursive FizzBuzz

import static java.lang.System.out;

Simple two-liner:

static void f(int a, int z) {
	out.println(a % 15 < 1 ? "FizzBuzz" : a % 3 < 1 ? "Fizz" : a % 5 < 1 ? "Buzz" : a);
	if (a < z) f(1 + a, z);
}

Without using the literal “FizzBuzz”:

static void g(int a, int z) {
  int x = 0;
  if (a % 3 < 1) { out.print("Fizz"); ++x; }
  if (a % 5 < 1) { out.print("Buzz"); ++x; }
  if (x < 1) out.print(a);
  out.println();
  if (a < z) g(1 + a, z);
}

Note that I use x<1 instead of x==0 to save a single character.

Advertisements