Is it a matter of a solution that works or the quality of the solution? The following Perl code works (don’t laugh; I’ve met programmers who code solutions this way), but would you hire the author?
$i = 1;
$val = { 3 = ‘Fizz’,
5 = ‘Buzz’,
6 = ‘Fizz’,
9 = ‘Fizz’,
10 = ‘Buzz’,
12 = ‘Fizz’,
15 = ‘FizzBuzz’,
18 = ‘Fizz’,
20 = ‘Buzz’,
21 = ‘Fizz’,
24 = ‘Fizz’,
25 = ‘Buzz’,
27 = ‘Fizz’,
30 = ‘FizzBuzz’,
33 = ‘Fizz’,
35 = ‘Buzz’,
36 = ‘Fizz’,
39 = ‘Fizz’,
40 = ‘Buzz’,
42 = ‘Fizz’,
45 = ‘FizzBuzz’,
48 = ‘Fizz’,
50 = ‘Buzz’,
51 = ‘Fizz’,
54 = ‘Fizz’,
55 = ‘Buzz’,
57 = ‘Fizz’,
60 = ‘FizzBuzz’,
63 = ‘Fizz’,
65 = ‘Buzz’,
66 = ‘Fizz’,
69 = ‘Fizz’,
70 = ‘Buzz’,
72 = ‘Fizz’,
75 = ‘FizzBuzz’,
78 = ‘Fizz’,
80 = ‘Buzz’,
81 = ‘Fizz’,
84 = ‘Fizz’,
85 = ‘Buzz’,
87 = ‘Fizz’,
90 = ‘FizzBuzz’,
93 = ‘Fizz’,
95 = ‘Buzz’,
96 = ‘Fizz’,
99 = ‘Fizz’,
100 = ‘Buzz’};
while ( $i 101 ) {
$p = $val-{$i} ? $val-{$i} : $i;
print “$p\n”; $i++
}
As far a recursion goes, I think whether you need to use it or not depends on what problems you’re solving. I don’t think we can assume that solutions aren’t optimal just because recursion is never used. I was probably 15 years in the business before I found a need to use recursion in the real world. Of course, when I did run across the need, at least I knew what it was.