# FizzBuzz Solution Dumping Ground

``````#include <iostream>
using namespace std;

int main () {
for (int i = 1; i <= 100; ++i) {
if (i%3 == 0) {
cout << "Fizz";
}
if (i%5 == 0) {
cout << "Buzz";
}
if (i%3 != 0 && i%5 != 0) {
cout << i;
}
cout << "\n";
}
return 0;
}``````

`for(var i=1;i<101;++i){var c=0^((i%3)===0)|0^((i%5)===0)<<1; console.log([i,"Fizz","Buzz","FizzBuzz"][c])}`

Python 1 liner:
`print "\n".join([(str(i) if i%3 else 'Fizz') if i%5 else ("Buzz" if i%3 else "FizzBuzz") for i in range(1,101)])`

``````using System;
using System.IO;
using System.Collections.Linq;
using Pointless.Libs.Algorithms;

class FuzzBizzDriver
{
public FuzzBizzr()
{
_fizzBuzzRepository = ExcedinglyPointlessAbstractFactory.Create`<FizzBuzzRepository>`();
}

public static async void Main(string[] args...)
{
var numbers = Enumerable.Range(0,1111111);
var awsmResult = DoFizzBuzz(numbers);
for (var i = 0; i < numbers.Count; i++)
{
Console.WriteLine(awsmResult);
}
}
{
var unnecessaryDict = new Dictionary<int, string>();
Parallel.ForEach(
numbers,
new ParallelOptions { MaxDegreeOfParallelism = 16 },
number => {
var resultString = string.Empty;
resultString += _fizzBuzzRepository.Fizz(number, resultString);
resultString += _fizzBuzzRepository.Buzz(number, resultString);

if (string.IsNullOrWhiteSpace(resultString))
{
resultString = number.ToString()
}

});
return unnecessaryDict.OrderBy(x => x.Key).Select(x => x.Value).ToList();

}
}``````

Here is what i made in 5min and a little JS

``````for (var i = 1; i < 101; i++) {
if (i % 3 == 0 && i % 5 === 0) {
console.log("FizzBuzz")
}
else if (i % 3 === 0) {
console.log("Fizz")
}
else if (i % 5 === 0){
console.log("Buzz")
}
else{
console.log(i)
}
}
``````

My solution in C (in my defense, I didn’t write all this by hand ):

``````#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main (){
printf("1 \n2 \nFizz \n4 \nBuzz \nFizz \n7 \n8 \nFizz \nBuzz \n11 \nFizz \n13 \n14 \nFizzBuzz \n16 \n17 \nFizz \n19 \nBuzz \nFizz \n22 \n23 \nFizz \nBuzz \n26 \nFizz \n28 \n29 \nFizzBuzz \n31 \n32 \nFizz \n34 \nBuzz \nFizz \n37 \n38 \nFizz \nBuzz \n41 \nFizz \n43 \n44 \nFizzBuzz \n46 \n47 \nFizz \n49 \nBuzz \nFizz \n52 \n53 \nFizz \nBuzz \n56 \nFizz \n58 \n59 \nFizzBuzz \n61 \n62 \nFizz \n64 \nBuzz \nFizz \n67 \n68 \nFizz \nBuzz \n71 \nFizz \n73 \n74 \nFizzBuzz \n76 \n77 \nFizz \n79 \nBuzz \nFizz \n82 \n83 \nFizz \nBuzz \n86 \nFizz \n88 \n89 \nFizzBuzz \n91 \n92 \nFizz \n94 \nBuzz \nFizz \n97 \n98 \nFizz \nBuzz \n");
}
``````
``````void loop(){
for(int i =1 ; i<=100; i++){
if (i% 15 == 0){
Log.i(TAG, "loop: FizzBuzz");
}else if(i % 3 == 0){
Log.i(TAG, "loop: Fizz");
}else if (i% 5 == 0){
Log.i(TAG, "loop: Buzz");
}else {
Log.i(TAG, "loop: " + i);
}
}
}
``````

Python 3:

``````print(*((x, 'Fizz', 'Buzz', 'FizzBuzz')[(not x%3) + (not x%5)*2] for x in range(1, 101)), sep='\n')
``````

or

``````print(*(x if x%5 and x%3 else 'FizzBuzz' if not x%3 and not x%5 else 'Fizz' if x%5 else 'Buzz' for x in range(1,101)), sep='\n')
``````

If nobody has done it yet in Excel (not VBA, simple Excel formulas):

1. fill series cells A1:A100 with numbers 1 through 100
2. fill down cells B1:B100 with formula `=IF(MOD(A1,15)=0,"FizzBuzz",IF(MOD(A1,5)=0,"Buzz",IF(MOD(A1,3)=0,"Fizz",A1)))`

Here’s a PHP solution that actually works

``````\$x=1;

while (\$x<=100)
{
if (\$x%3==0 and \$x%5==0)
{
print "<p>\tfizzbuzz</p>\n";
}
elseif (\$x%3==0)
{
print "<p>\tfizz</p>\n";
}
elseif (\$x%5==0)
{
print "<p>\tbuzz"."</p>\n";
}
else
{
print "<p>\$x</p>\n";
}

\$x++;
}``````

I see no continue being used.

``````for (int i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("Fizz-Buzz {0}", i);
continue;
}
if (i % 3 == 0 && i % 5 != 0)
{
Console.WriteLine("Fizz {0}", i);
continue;
}
if (i % 3 != 0 && i % 5 == 0)
{
Console.WriteLine("Buzz {0}", i);
continue;
}
Console.WriteLine(i);
}``````

In C I would use this beautiful oneliner:

``````for(int t=1;t<=100;t++)
( (!(t%3)&&printf("Fizz")) + ((t%5==0)&&printf("Buzz")) || printf("%d", t)), putchar('\n');
``````

Just 2 modulos needed, no continue, no caching, just pure misuse of logical expression evaluation mechanics.

1 Like

Oh man. The article was right.

1 Like

One solution in Erlang with recursion.

``````-module(fizzbuzz).
-export([print/0]).

print() ->
FizzBuzzList = eval(100, []),
lists:foreach(fun(Item)->io:format("~p~n", [Item]) end, FizzBuzzList).

eval(Count, Acc) when Count /= 0 ->
Remainders = {Count rem 3, Count rem 5},
NewAcc = case Remainders of
{0, 0} -> ["FizzBuzz"|Acc];
{0, _} -> ["Fizz"|Acc];
{_, 0} -> ["Buzz"|Acc];
{_, _} -> [Count|Acc]
end,
eval(Count-1, NewAcc);

eval(0, Acc) ->
Acc.

``````
1 Like

Old but gold, vb6:

`````` Dim i As Integer
Open "C:\output.txt" For Append As #1 ' VB6 does not have native console window support
For i=1 To 100
If i Mod 3 = 0 Then Print #1, "Fizz"
If i Mod 5 = 0 Then Print #1, "Buzz"
If i Mod 15 <> 0 Then Print #1, Cstr(i)
Next
Close #1
``````

Also remembering that VB6 does not have an official console mode support, it’s necessary to waste two lines of code, openning and closing the text file used as Output.

1 Like

My 2 cents in Perl. Not the shortest, but it is among the most compact and still readable, doesn’t cheat using a module that does the work and is not exposed, and most importantly, it works CORRECTLY (version 5.10 and above due to use of “say” instead of “print”). Yes, it is amazing how many solutions here do not follow the actual specifications (or plain don’t work):

``````while (\$i++ < 100){
my \$out;
\$out  = "Fizz" unless (\$i%3);
\$out .= "Buzz" unless (\$i%5);
say "\$out" || "\$i";
}``````

This one is simpler, more readable, and no ternary ops:

``````while (\$i++ < 100){
my \$out;
\$out  = "Fizz" unless (\$i%3);
\$out .= "Buzz" unless (\$i%5);
say "\$out" || "\$i";
}

# ... and oh, it works correctly according to the specs.  One is not supposed to print the integer if the strings "fizz", "buzz", or "fizzbuzz" are printed (many solutions in this page disregard that point.``````

Of course we couldn’t forget our both loved and hated VBA in all its glory

Normal Loop:

``````Sub FizzBuzz(n As Integer)
Dim i As Integer

For i = 1 To n
If i Mod 3 = 0 And i Mod 5 = 0 Then
Debug.Print "FizzBuzz"
ElseIf i Mod 3 = 0 Then
Debug.Print "Fizz"
ElseIf i Mod 5 = 0 Then
Debug.Print "Buzz"
Else
Debug.Print i
End If
Next i

End Sub
``````

And some recursive:

``````Sub RecursiveFizzBuzz(n As Integer)

If n > 1 Then
RecursiveFizzBuzz (n - 1)
If n Mod 3 = 0 And n Mod 5 = 0 Then
Debug.Print "FizzBuzz"
ElseIf n Mod 3 = 0 Then
Debug.Print "Fizz"
ElseIf n Mod 5 = 0 Then
Debug.Print "Buzz"
Else
Debug.Print n
End If
Else
Debug.Print n
End If

End Sub
``````

Now imagine that poor soul that made Roller Coaster 2, ALL in assembly…

1 Like

Let’s go python:

``````    def FizzBuzz(highestNumber, numbers, words):
for i in range(1, highestNumber + 1):
out = ""
for number, word in zip(numbers, words):
if i % number == 0:
out += word
print(out if out else i)

FizzBuzz(100, [3,5], ["Fizz", "Buzz"])
``````

Boom done. Reusable for different numbers and words

1 Like