[ create a new paste ] login | about

Recent implementations of FizzBuzz, in:
[ C | C++ | Haskell | Lua | PHP | Perl | Python | Ruby ]

C:
pasted on May 22:
#include <stdio.h>
#include <stdlib.h>

void doFizzBuzz( unsigned i ) {
	const char *const fizz = i%3 == 0 ? "fizz" : "";
	const char *const buzz = i%5 == 0 ? "buzz" : "";

	if( !( *fizz | *buzz ) ) {
		printf( "%u\n", i );
	} else {
		printf( "%s%s\n", fizz, buzz );
	}
}

int main() {
        unsigned i;

	for( i = 1; i <= 100; i += 1 ) {
		doFizzBuzz( i );
	}

	return EXIT_SUCCESS;
}
view (23 lines, 100 lines of output)
needles - pasted on May 21:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
int main(void)
{
        int i;
	for(i=1;i<=100;i++)
	{
		if(i%3==0)
		    printf("Fizz");
		if(i%5==0)
		    printf("Buzz");
		else if (i % 3)
		    printf("%d",i);
                puts("");
	}
	return 0;
}
view (16 lines, 100 lines of output)


C++:
pasted yesterday:
#include <iostream>
using namespace std;

int main()
{

    int num = 1; //プリントする数を格納する変数

    int multi_3 = 3; //3の倍数
    int multi_5 = 5; //5の倍数


    //1から100まで繰り返す
    for( num = 1; num <= 100; num++)
    {
        //3と5の両方の倍数のときFizzBuzzを出力
        if( num % multi_3 == 0 && num % multi_5 == 0)
        {
            printf("FizzBuzz\n");
        }
        
        //3の倍数のときFizzを出力
        else if( num % multi_3 == 0 )
        {
            printf("Fizz\n");
        }

        //5の倍数のときBuzzを出力
        else if( num % multi_5 == 0 )
        {
            printf("Buzz\n");
        }

        //numを出力する
        else
        {
            printf("%d\n", num);
        }

    }

    return 0;
}
view (43 lines, 100 lines of output)
pasted yesterday:
#include <iostream>
using namespace std;

int main()
{

    int num = 1; //プリントする数を格納する変数

    int multi_3 = 3; //3の倍数
    int multi_5 = 5; //5の倍数


    //1から100まで繰り返す
    for( num = 1; num <= 100; num++)
    {
        //3と5の両方の倍数のときFizzBuzzを出力
        if( num % multi_3 == 0 && num % multi_5 == 0)
        {
            printf("FizzBuzz\n");
        }
        
        //3の倍数のときFizzを出力
        else if( num % multi_3 == 0 )
        {
            printf("Fizz\n");
        }

        //5の倍数のときBuzzを出力
        else if( num % multi_5 == 0 )
        {
            printf("Buzz\n");
        }

        //numを出力する
        else
        {
            printf("%d\n", num);
        }

    }

    return 0;
}
view (43 lines, 100 lines of output)


Haskell:
pasted on May 15:
1
2
3
4
5
6
7
import Data.List
fizzBuzzList :: Int -> [String]
fizzBuzzList n = zipWith3(\i f b -> if f++b == "" then show i else f++b)
    [1..n] (cycle ["","","Fizz"]) (cycle [ "", "", "", "", "Buzz"])

main :: IO()
main = mapM_ putStrLn $ fizzBuzzList 100
view (7 lines, 100 lines of output)
pasted on Mar 7:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fzzbzz :: Int -> String -> (String, Int) -> (String, Int)
fzzbzz m sound (str, x) = (str ++ if mod x m == 0 then sound else "", x)

fix :: (String, Int) -> String
fix ("", x) = show x
fix (st, _) = st

fizzbuzz :: [(String, Int)] -> Int -> String
fizzbuzz sounds top
    | top <= 0  = ""
    | otherwise = foldr (\x y -> x ++ "\n" ++ y) [] (map fizzer numbers)
    where fizzer = (foldl (.) fix (reverse [fzzbzz i s | (s, i) <- sounds]))
          numbers = take top (zip (cycle [""]) [1..])

main = putStr (fizzbuzz [("Fizz", 3), ("Buzz", 5)] 100)
view (15 lines, 100 lines of output)


Lua:
pasted on Dec 20:
1
2
3
4
5
6
7
for i=1, 100 do
    output = ''
    if i%3 == 0 then output = 'Fizz' end
    if i%5 == 0 then output = output..'Buzz' end
    if output == '' then output = i end
    print(output)
end
view (7 lines, 100 lines of output)
pasted on Dec 20:
1
2
3
4
5
6
7
for i=1, 100 do
    output = ''
    if i%3 == 0 then output = 'Fizz' end
    if i%5 == 0 then output = output..'Buzz' end
    if output == '' then output = i end
    print(output)
end
view (7 lines, 100 lines of output)


PHP:
pasted on Apr 24:
1
2
3
4
5
6
7
8
9
10
<?php
  $input=100;
  for($i=1;$i<=$input;$i++){
    $item=$i%3===0?"Fizz":"";
    $item=$i%5===0?$item."Buzz":$item;
    $item=$item===""?$i:$item;
    $item.="\n";
    echo $item;
  }
?>
view (10 lines, 100 lines of output)
pasted on Apr 24:
1
2
3
4
5
6
7
8
9
<?php
  for($i=1;$i<=100;$i++){
    $item=$i%3===0?"Fizz":"";
    $item=$i%5===0?$item."Buzz":$item;
    $item=$item===""?$i:$item;
    $item.="\n";
    echo $item;
  }
?>
view (9 lines, 100 lines of output)


Perl:
pasted on Apr 4:
1
2
@a = ('','Fizz','Buzz','FizzBuzz');
print join "\n", map{ $a[0] = $_;  $a[(!($_%3) + !( $_%5)*2)]  }(1..100);
view (2 lines, 100 lines of output)
pasted on Apr 4:
1
2
@a = ('','Fizz','Buzz','FizzBuzz');
print join "\n", map{ $a[0] = $_; $a[(!($_%3) + !( $_%5)*2)]  }(1..100);
view (2 lines, 100 lines of output)


Python:
pasted on Mar 29:
1
2
3
4
5
6
7
8
9
def fbr(n):
    return (
        ("" if n%3 else "fizz") +
        ("" if n%5 else "buzz")
    ) or str(n)

if __name__ == "__main__":
    for i in range(100):
        print fbr(i+1)
view (9 lines, 100 lines of output)
pasted on Mar 16:
#! /usr/bin/env python3
# fizzbuzz.py

def divisor_response(divisor, response):
    # say fizz if the number is divisible by 3
    return lambda n: "" if n % divisor != 0 else response

def arbitrary_divisor_responses(d_responses):
    # combine fizzbuzz responses in a specific order
    # or say the number if none of them apply
    return lambda n: "".join(
        [divisor_response(*s)(n) for s in d_responses] ) or str(n)

def fizzbuzz(n):
    # actual fizzbuzz
    return arbitrary_divisor_responses([
        (3, "fizz"), (5, "buzz")])(n)

def fb_start_end(start, end):
    # fizzbuzz within a range
    return "\n".join([
        fizzbuzz(n) for n in range(start, end+1)])

if __name__ == "__main__":
    print(fb_start_end(1, 100))
view (26 lines, 100 lines of output)


Ruby:
pasted on May 17:
1
2
3
4
5
6
7
8
9
10
11
for num in 1..100 do
 if num % 15 == 0
   puts "FizzBuzz"
 elsif num % 3 == 0
   puts "Fizz"
 elsif num % 5 == 0
   puts "Buzz"
 else
   puts num
 end
end
view (12 lines, 100 lines of output)
pasted on Feb 7:
1
2
3
4
5
6
7
8
9
10
11
for num in 1..100 do
 if num % 15 == 0
   puts "FizzBuzz"
 elsif num % 3 == 0
   puts "Fizz"
 elsif num % 5 == 0
   puts "Buzz"
 else
   puts num
 end
end
view (11 lines, 100 lines of output)