FizzBuzz Solution Dumping Ground

some of these are cringe.

for (int i = 1; i <= 100; i++)
	{
		int mod3 = i%3, mod5 = i%5;
		if (!mod3 || !mod5)
		{
			if (!mod3) cout << "Fizz";
			if (!mod5) cout << "Buzz";
		}     		
		else cout << i;
     		cout << endl;
	}

Here is the python version approach. This is my first actual working code since I started learning about couple of days

for x in range(1,22):
    #print x
    if x % 5 == 0:
        print x,"buzz"
    elif x % 3 ==0:
        print x,"fuzz"
    else:
        print x

Output:

SimpleTest.py’, wdir='C:/Users/rakz)
1
2
3 fuzz
4
5 buzz
6 fuzz
7
8
9 fuzz
10 buzz
11
12 fuzz
13
14
15 buzz
16
17
18 fuzz
19
20 buzz
21 fuzz

F# Version with tail-recursion… Just because.

    let fizzBuzzList n =
        let rec fizzBuzz n acc =
            match n with
            | 0 -> acc
            | n when n % 3 = 0 && n % 5 = 0 -> fizzBuzz (n-1) ("FizzBuzz" :: acc)
            | n when n % 3 = 0 -> fizzBuzz (n-1) ("Fizz" :: acc)
            | n when n % 5 = 0 -> fizzBuzz (n-1) ("Buzz" :: acc)
            | _ as n -> fizzBuzz (n-1) (string n :: acc)
        fizzBuzz n []

    let printFizzBuzz n =
        fizzBuzzList n
        |> List.iter (fun s -> printfn "%s" s)

    printFizzBuzz 100000

Recrusive Ruby Solution

def fizzbuzz(n)
  return if n > 100
  out = "#{n}: "
  out += "Fizz" if (n % 3 == 0)
  out += "Buzz" if (n % 5 == 0)
  puts out
  fizzbuzz(n+1)
end

fizzbuzz(1)

It is really easy with Haskell and list comprehensions:

main :: IO ()
main = putStrLn $ unlines fizzbuzz

fizzbuzz :: [String]
fizzbuzz = [if x `dividable` 3
              then if x `dividable` 5
                      then "Fizzbuzz"
                      else "Fizz"
              else "Buzz"
            | x <- [1..100], x `dividable` 5 || x `dividable` 3]

dividable :: Int -> Int -> Bool
dividable lop rop = (lop `mod` rop) == 0

A solution in F#

let (|Fizz|Buzz|FizzBuzz|Other|) x = 
    if x % 15 = 0 then FizzBuzz
    else if x % 5 = 0 then Fizz
    else if x % 3 = 0 then Buzz
    else Other

[1..100]
|> List.iter (fun x -> printfn "%s" (match x with
                                     | Fizz -> "Fizz"
                                     | Buzz -> "Buzz"
                                     | FizzBuzz -> "FizzBuzz"
                                     | _ -> x.ToString()))
	for (i = 1; i <= 100; i++) {

		if (i%3 == 0 && i%5 == 0) {
			document.write(" - FizzBuzz <br />");
		} else if (i%3 == 0) {
			document.write(" - Fizz <br />");
		} else if (i%5 == 0) {
			document.write(" - Buzz <br />");
		} else {
			document.write(" - " + i + "<br />");
		}
	}

I am 12 and it took me a bit longer than 5 min because I wrote html.

1 Like

Oracle PL/SQL

DECLARE
   msg_ VARCHAR2(8);
BEGIN
   FOR rec_ IN (
      SELECT LEVEL AS lvl
        FROM DUAL
      CONNECT BY LEVEL <= 100
   ) LOOP
      SELECT DECODE(MOD(rec_.lvl, 15),
                    0, 'FizzBuzz',
                    3, 'Fizz',
                    5, 'Buzz',
                    6, 'Fizz',
                    9, 'Fizz',
                    10, 'Buzz',
                    TO_CHAR(rec_.lvl)) INTO msg_
        FROM DUAL;
      dbms_output.put_line(msg_);
  END LOOP;
END;
/

REM before you judge, I am a senior level executive, not a programmer
REM I did some basic programming on Apple ii’s back in the 80’s
REM It took me about 10 mins, mostly shaking the rust off and reformatting some
REM of the earlier attempts

REM “FizzBuzz program”

for i = 1 to 100
if i mod 3 = 0 then REM i is evenly div by 3
print “Fizz”; REM suppress newline
end if
if i mod 5 = 0 then REM i is evenly div by 5
print “Buzz” REM with newline
else
print REM adds newline to "Fizz"
end if
REM we are done with div by 3, 5 and 15
if i mod 3 > 0 and i mod 5 > 0 then REM not div by 3 or 5
print i
end if
next i

REM I would fail most of the attempts listed here in an interview situation for failing to
REM properly document the code. Plain language documentation is important. Apologies
REM for not formatting the code. I didn’t know how :slight_smile:

1 Like

Swift…

for i in 1 ... 100 {
   let result = (i % 3 == 0 ? "Fizz" : "") + (i % 5 == 0 ? "Buzz" : "")
   print(result.isEmpty ? String(i) : result)
}
#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
{
    private readonly IFizzBuzzRepository _fuzzBizzRepository;
    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);
        }
        Console.ReadKey();
    }
    private async Task<IList<string>> DoFizzBuzz(IEnumerable numbers)
    {
        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()
                }

                unnecessaryDict.Add(number);
            });
        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 :stuck_out_tongue: ):

#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 :slight_smile:

$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++;
}