FizzBuzz in APL

Aniket Bhattacharyea
codeburst
Published in
3 min readSep 17, 2018

--

Print all the numbers from 1 to 100, but if the number is divisible by 3, print “Fizz”, if it’s divisible by 5, print “Buzz”, and if it’s divisible by both, print “FizzBuzz”

This famous problem is known as “FizzBuzz challenge”, and is a famous one in many interview questions. While it can be written easily with a few if-else, the real challenge is to write it using as few characters as possible. And what else can be a better contender for one-liners than APL?

So, let me first describe the algorithm. Forget about the all the numbers from 1 to 100 part. Suppose you have only one number, say 50. How do you proceed?

We do it in this way —

First we divide the number by 15, 3 and 5 and take the residue

      15 3 5 | 50
5 2 0

The | (residue) operator divides 50 by 15, 3 and 5 and gives the remainders as an array.

Now we find out which of these remainders is 0. So we compare the array with 0

      0=15 3 5|50
0 0 1

This comparison return an array in which a 0 corresponds to a non-zero remainder and 1 corresponds to a 0 remainder.

Now we just find out the index of 1, so that we know which of the remainders is 0. We use the dyadic form of ⍳

      (0=15 3 5|50)⍳1
3

This returns the position of 1(arrays are 1 indexed)

If 1 does not occur in the array (i.e. none of the remainders are 0), it returns 4 (length of array + 1)

If 1 occurs more than once, it returns the first position

Now we create an array of what we want to print

      'FizzBuzz' 'Fizz' 'Buzz', 50
FizzBuzz Fizz Buzz 50

This creates an array of 4 elements.

Now we just pass the index calculated in the previous step in this array

      ('FizzBuzz' 'Fizz' 'Buzz', 50)[(0=15 3 5|50)⍳1]
Buzz

So, if our number is divisible by 15, the index will be 1 and we will get ‘FizzBuzz’, if it’s divisible by 3, the index will be 2 and we will get ‘Buzz’ and so on. And if it’s not divisible by any one, the index will be 4, and we will get back the original number.

Now we just make it a function and call it on each number from 1 to 100. For that, we use ⍳ to generate all numbers from 1 to 100 and use ¨ to call it on each of the numbers

      {(‘FizzBuzz’ ‘Fizz’ ‘Buzz’,⍵)[(0=15 3 5|⍵)⍳1]}¨⍳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

This uses the anonymous function feature of Dyalog APL.

✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.

--

--

I’m a Mathematics student from Kolkata, India. I’m a self taught hobbyist programmer.