Skip to content

Instantly share code, notes, and snippets.

@zyro
Created November 15, 2015 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zyro/e60e75ef3a3c3f08e5ef to your computer and use it in GitHub Desktop.
Save zyro/e60e75ef3a3c3f08e5ef to your computer and use it in GitHub Desktop.
# 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.
@zyro
Copy link
Author

zyro commented Nov 20, 2015

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

@Steffan153
Copy link

Using max is shorter:

for i<-1..?d,do: IO.puts max"#{i}",(rem(i,3)<1&&"Fizz"||"")<>(rem(i,5)<1&&"Buzz"||"")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment