Skip to content

Instantly share code, notes, and snippets.

@yuutayamada yuutayamada/fizzbuzz.nim
Last active Oct 4, 2017

Embed
What would you like to do?
fizz buzz nim static version
type
fbEnum = enum
raw = 0
fizz = "Fizz\n" # 1
buzz = "Buzz\n" # 2
fizzBuzz = "FizzBuzz\n" # 3
template isFizz(i: typed): bool = i mod 3 == 0
template isBuzz(i: typed): bool = i mod 5 == 0
proc computeFizzBuzz(i: int): fbEnum =
((if i.isFizz(): fizz else: raw).uint8 +
(if i.isBuzz(): buzz else: raw).uint8).fbEnum
static:
var str = ""
for i in 1 .. 600_000:
str.add case computeFizzBuzz(i)
of fizz: $fizz
of buzz: $buzz
of fizzBuzz: $fizzBuzz
else: $i & "\n"
const outstr: string = str
write stdout, outstr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.