Skip to content

Instantly share code, notes, and snippets.

@zyro zyro/1-fizzbuzz.exs
Created Nov 15, 2015

Embed
What would you like to do?
# for x<-1..?d,do: IO.puts(if rem(x,3)==0 or rem(x,5)==0,do: (if rem(x,3)==0,do: "Fizz",else: "")<>(if rem(x,5)==0,do: "Buzz",else: ""),else: x)
# for x<-1..?d,do: IO.puts(if rem(x,3)>0 and rem(x,5)>0,do: x,else: (if rem(x,3)>0,do: "",else: "Fizz")<>(if rem(x,5)>0,do: "",else: "Buzz"))
# for x<-1..?d,do: IO.puts(if rem(x,3)>0&&rem(x,5)>0,do: x,else: (if rem(x,3)>0,do: "",else: "Fizz")<>(if rem(x,5)>0,do: "",else: "Buzz"))
# for x<-1..?d,do: IO.puts if rem(x,3)>0&&rem(x,5)>0,do: x,else: (if rem(x,3)>0,do: "",else: "Fizz")<>(if rem(x,5)>0,do: "",else: "Buzz")
# for x<-1..?d,do: IO.puts if rem(x,3)>0&&rem(x,5)>0,do: x,else: "#{(if rem(x,3)<1,do: "Fizz")}#{(if rem(x,5)<1,do: "Buzz")}"
# for x<-1..?d,do: IO.puts if rem(x,3)>0&&rem(x,5)>0,do: x,else: "#{rem(x,3)<1&&"Fizz"||""}#{rem(x,5)<1&&"Buzz"||""}"
# for x<-1..?d,do: IO.puts if rem(x,3)>0&&rem(x,5)>0,do: x,else: (rem(x,3)<1&&"Fizz"||"")<>(rem(x,5)<1&&"Buzz"||"")
# for x<-1..?d,do: IO.puts rem(x,3)>0&&rem(x,5)>0&&x||(rem(x,3)<1&&"Fizz"||"")<>(rem(x,5)<1&&"Buzz"||"")
# for x<-1..?d,do: IO.puts (z=(rem(x,3)<1&&"Fizz"||"")<>(rem(x,5)<1&&"Buzz"||""))&&z==""&&x||z
# Current best, 89 characters:
for x<-1..?d,z=(rem(x,3)<1&&"Fizz"||"")<>(rem(x,5)<1&&"Buzz"||""),do: IO.puts z==""&&x||z
  • ?d == 100.
  • z evaluates to at least "", which is a truthy value allowing the print to run.
  • Remainder <1 saves 1 character over ==0.
  • Operator precedence used here: == or <, then &&, then ||.
  • Omitting brackets on IO.puts saves 1 character.
  • ,do: is 1 character shorter than do end.
@henrik

This comment has been minimized.

Copy link

commented Nov 20, 2015

88! Using a twist on your own <1 trick:

for x<-1..?d,z=(rem(x,3)<1&&"Fizz"||"")<>(rem(x,5)<1&&"Buzz"||""),do: IO.puts z>""&&z||x
@henrik

This comment has been minimized.

Copy link

commented Nov 20, 2015

87!

for x<-1..?d,z=(rem(x,3)<1&&"Fizz"||"")<>(rem(x,5)<1&&"Buzz"||""),do: IO.puts""<z&&z||x
@emson

This comment has been minimized.

Copy link

commented Nov 20, 2015

oh wow... I'll have to update FizzBuzz ... meh ;o)

@zyro

This comment has been minimized.

Copy link
Owner Author

commented Nov 20, 2015

I love the z>"" to ""<z flip to get rid of the preceding whitespace!

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.