Skip to content

Instantly share code, notes, and snippets.

@yuutayamada
Last active October 4, 2017 18:05
Show Gist options
  • Save yuutayamada/fa9415503048ce58b6f47d6a049a8c81 to your computer and use it in GitHub Desktop.
Save yuutayamada/fa9415503048ce58b6f47d6a049a8c81 to your computer and use it in GitHub Desktop.
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