Emacs, scripting and anything text oriented.

Nim: Fizz-Buzz test

My attempt at FizzBuzz in Nim.
Kaushal Modi

Today I came across this FizzBuzz attempt for Nim, so I thought of giving it a try too.

Here’s how Fizz buzz is defined on Wikipedia:

Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word “fizz”, and any number divisible by five with the word “buzz”.

And it’s also one of those basic programming problems (labeled as interview questions) that’s written in many languages. Here’s how Rosetta Code defines this programming task —

Write a program that prints the integers from 1 to 100 (inclusive).

But:

  • for multiples of three, print Fizz (instead of the number)
  • for multiples of five, print Buzz (instead of the number)
  • for multiples of both three and five, print FizzBuzz (instead of the number)

I am not doing anything radical in this post.. just recording my attempt at FizzBuzz using Nim 😄.

for i in 1 .. 100:
  var str = $i
  if (i mod 3) == 0:
    str = "Fizz"
    if (i mod 5) == 0:
      str.add("Buzz")
  elif (i mod 5) == 0:
    str = "Buzz"
  echo str

See the output

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


Versions used: nim 230692a22f9

If you have written a response to this, enter your response post's URL below.

Or, you can send a "comment" webmention (it's OK if you don't know what that means). When asked about your website on an IndieAuth login screen, simply type https://commentpara.de.

Markdown Support**bold**, _italics_, ~~strikethrough~~, [descr](link), `monospace`, ```LANG\nline1\nline2\n``` (Yep, multi-line code blocks too, with syntax highlighting!), auto-hyperlinking.

Webmentions #