FizzBuzz Solution Dumping Ground

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

Python, could be more compact but this way it is more clear what the program does and less computation only mod two times instead of four.

for i in range(0,101):
    
    fizz = i % 5 == 0
    buzz = i % 3 == 0
    
    if fizz and buzz:
        print("FizzBuzz")
        continue
    if fizz:
        print("Fizz")
        continue
    if buzz:
        print("Buzz")
        continue
    print(i)

In java, but in functional paradigm (don’t do this at work).

public static void main(String[] args) {
	final String result = IntStream
			.rangeClosed(1, 100)
			.mapToObj(Scratch::fizzBuzz)
			.collect(Collectors.joining(", "));

	System.out.print(result);
}

static String fizzBuzz(int n) {
	return buzz(n)
			.andThen(fizz(n))
			.apply(Function.identity())
			.apply(String.valueOf(n));
}

static Function<Function<String, String>, Function<String, String>> buzz(int n) {
	return test(n, 5, "buzz");
}

static Function<Function<String, String>, Function<String, String>> fizz(int n) {
	return test(n, 3, "fizz");
}

static Function<Function<String, String>, Function<String, String>> test(int number, int divisor, String match) {
	return f -> d -> test(number, divisor, match, f, d);
}

static String test(int number, int divisor, String match, Function<String, String> next, String defaultValue) {
	return number % divisor == 0
			? match + next.apply("")
			: next.apply(defaultValue);
}
2 Likes

And since no one seems to have picked MUMPS in thirteen years of responses:

F I=1:1:100 W !,$S(I#15=0:"Fizzbuzz",I%3=0:"Fizz",I%5=0:"Buzz",1:I)

3 Likes

Surprised no one posted this efficient solution so far.

I’ll post it in PHP I guess, but the concept works in most languages.

function FizzBuzz($limit = 100, $FizzMultiple=3, $BuzzMultiple=5){
    for($i=0; $i < $limit; $i++){
        echo $i .': ';
        switch(true){
            case ($i % $FizzMultiple == 0) : echo "Fizz";
            case ($i % $BuzzMultiple == 0) : echo "Buzz"; break;
            default: break;
        }
        echo PHP_EOL;
    }
}
FizzBuzz(1500);


function FizzBuzz2($limit = 100, $FizzMultiple=3, $BuzzMultiple=5){
    for($i=0; $i < $limit; $i++){
        echo $i .': ';
        if($i % $FizzMultiple == 0)
            echo "Fizz";
        if($i % $BuzzMultiple == 0)
            echo "Buzz";
        echo PHP_EOL;
    }
}
FizzBuzz2(1500);
1 Like

Well I read the article and knew I had to dump my JS solution for it :slight_smile:

var i;
for (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);
    }
}
2 Likes

I tried this simple code :smiley:

for number in range(101):
    if number%15 == 0 :
       print("FizzBuzz")
    elif number%5 == 0:
        print("Buzz")
    elif number%3 == 0: 
        print("Fizz")
    else:
        print(number)
3 Likes

Surprised nobody has posted this here yet…

1 Like
for num in range(1,100):
    if num % 3 == 0:
        print("Fizz", num)
    elif num % 5 == 0:
        print("Buzz", num)
    elif num % 3 == 0 and num % 5 == 0:
        print("FizzBuzz", num)
    elif num % 1 == 0:
        print("Prime", num)
2 Likes

Thank you for liking my code. I also wrote two different paper, scissors, rock program but was difficult at the start and abstract but I put mind to work and think and think with music. I am a computer science major in 4-year computer science degree program. I am really dedicating as much time to learn Python3.9.4, C++, C#, R, and several others. My main programming languages to learn is Python 3 for data science/ml/games, C# for Unity3D, C++ for Unreal Engine, R for statistical, mathematical, and graphical computing. Also, I heard software design documents are effective in computer programming but I need tips.

Also, wrote several python programs and couple (more than 10 i think) of C++ program and still learning…

2 Likes

Java Solution

class Main {
  public static void main(String[] args) {
    int i = 0;
    while (i != 101) {
      if ((i % 3 == 0)&(i % 5 != 0)){
        System.out.println("Fizz");
      } else if ((i % 3 != 0)&(i % 5 == 0)){
        System.out.println("Buzz");
      } else if ((i % 3 == 0)&(i % 5 == 0)){
        System.out.println("FizzBuzz");
      } else {
        System.out.println(i);
      }
      i++;
    }
  }
}

Python
[COMING SOON]

1 Like