NYCJUG/FizzBuzz
[From “Don’t Overthink FizzBuzz” at http://weblog.raganwald.com/2007/01/dont-overthink-fizzbuzz.html]
The "FizzBuzz Problem", referred to here, is defined this way (from this article about how to find "Developers who Grok Coding"):
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
Among the comments, we found solutions in many languages, including the following one in J.
FizzBuzz solution in J
FB=:((0 i.~15 3 5|]){::'FizzBuzz';'Fizz';'Buzz';":)"0 FB i.100
This is a tacit or functional solution, as the input parameter (number of integers to print) is not specified in the function FB. The function FB is defined in the first line, and invoked in the second line. The right argument of FB in the second line simply generates all the integers between 0 and 99.
# posted by Teledon : 10:21 AM
A Hint About One Advantage of a Solution in J
The solution above has an admirable clarity and succinctness to it but the interactive, array-handling capability of J is also useful to quickly verify that this solution appears to be correct. Below, we apply the verb above to an Nx15 table of numbers, suitably padded, so we can see the solutions line up neatly in columns.
(>.100%15),15*>.100%15 NB. Checking the result is important, too. 7 105 7 15$105{.FB&.>>:i.100 +--+--+----+--+----+----+--+--+----+----+--+----+--+--+--------+ |1 |2 |Fizz|4 |Buzz|Fizz|7 |8 |Fizz|Buzz|11|Fizz|13|14|FizzBuzz| +--+--+----+--+----+----+--+--+----+----+--+----+--+--+--------+ |16|17|Fizz|19|Buzz|Fizz|22|23|Fizz|Buzz|26|Fizz|28|29|FizzBuzz| +--+--+----+--+----+----+--+--+----+----+--+----+--+--+--------+ |31|32|Fizz|34|Buzz|Fizz|37|38|Fizz|Buzz|41|Fizz|43|44|FizzBuzz| +--+--+----+--+----+----+--+--+----+----+--+----+--+--+--------+ |46|47|Fizz|49|Buzz|Fizz|52|53|Fizz|Buzz|56|Fizz|58|59|FizzBuzz| +--+--+----+--+----+----+--+--+----+----+--+----+--+--+--------+ |61|62|Fizz|64|Buzz|Fizz|67|68|Fizz|Buzz|71|Fizz|73|74|FizzBuzz| +--+--+----+--+----+----+--+--+----+----+--+----+--+--+--------+ |76|77|Fizz|79|Buzz|Fizz|82|83|Fizz|Buzz|86|Fizz|88|89|FizzBuzz| +--+--+----+--+----+----+--+--+----+----+--+----+--+--+--------+ |91|92|Fizz|94|Buzz|Fizz|97|98|Fizz|Buzz| | | | | | +--+--+----+--+----+----+--+--+----+----+--+----+--+--+--------+
A J Solution Using Agenda
Here's another J solution using the "agenda" conjunction:
fb=: (]`('Fizz'"_)`('Buzz'"_)`('FizzBuzz'"_) @. ([: #. 0 = 5 3 | ])"0)&.> 7 15 $ fb >:i.105 +--+--+----+--+----+----+--+--+----+----+---+----+---+---+--------+ |1 |2 |Fizz|4 |Buzz|Fizz|7 |8 |Fizz|Buzz|11 |Fizz|13 |14 |FizzBuzz| +--+--+----+--+----+----+--+--+----+----+---+----+---+---+--------+ |16|17|Fizz|19|Buzz|Fizz|22|23|Fizz|Buzz|26 |Fizz|28 |29 |FizzBuzz| +--+--+----+--+----+----+--+--+----+----+---+----+---+---+--------+ |31|32|Fizz|34|Buzz|Fizz|37|38|Fizz|Buzz|41 |Fizz|43 |44 |FizzBuzz| +--+--+----+--+----+----+--+--+----+----+---+----+---+---+--------+ |46|47|Fizz|49|Buzz|Fizz|52|53|Fizz|Buzz|56 |Fizz|58 |59 |FizzBuzz| +--+--+----+--+----+----+--+--+----+----+---+----+---+---+--------+ |61|62|Fizz|64|Buzz|Fizz|67|68|Fizz|Buzz|71 |Fizz|73 |74 |FizzBuzz| +--+--+----+--+----+----+--+--+----+----+---+----+---+---+--------+ |76|77|Fizz|79|Buzz|Fizz|82|83|Fizz|Buzz|86 |Fizz|88 |89 |FizzBuzz| +--+--+----+--+----+----+--+--+----+----+---+----+---+---+--------+ |91|92|Fizz|94|Buzz|Fizz|97|98|Fizz|Buzz|101|Fizz|103|104|FizzBuzz| +--+--+----+--+----+----+--+--+----+----+---+----+---+---+--------+
Since this returns potentially mixed arrays - combining numbers and characters - we define it to return boxed results.