Created
August 29, 2014 01:39
-
-
Save plombardi89/4bd7d13b1def7d61bfce to your computer and use it in GitHub Desktop.
Recursive Fizzbuzz
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
// Performs the FizzBuzz test using recursion. I intend to use this more often when interviewing job candidates to find out if they | |
// even have a basic grasp of recursion. | |
def fizzbuzz(int max) { | |
fizzbuzz(1, max) | |
} | |
def fizzbuzz(int current, int max) { | |
if (current == max) | |
return | |
def out = "" | |
if (current % 3 == 0) | |
out += 'fizz' | |
if (current % 5 == 0) | |
out += 'buzz' | |
println (out ?: current) | |
fizzbuzz(current + 1, max) | |
} | |
fizzbuzz(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment