[ create a new paste ] login | about

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

C:
pasted 3 hours ago:
1
2
3
4
5
6
7
8
9
int main(void)
{
	char *s[] = { "%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n" };
	int i;

	for (i = 1; i <= 100; i++)
		printf(s[((i % 3) == 0) | ((i % 5) == 0)<<1], i);
	return 0;
}
view (9 lines, 100 lines of output)
pasted on May 29:
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>

int main(void)
{
	char *s[] = { "%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n" };
	int i;

	for (i = 1; i <= 100; i++)
		printf(s[((i % 3) == 0) | ((i % 5) == 0)<<1], i);
	return 0;
}
view (11 lines, 100 lines of output)


C++:
pasted on May 27:
#include <string>

typedef unsigned int uint;

bool divisible_by(uint n, uint denominator) {
    return n % denominator == 0;
}

int main() {
    std::string s = std::string();
    for (uint i = 1; i <= 100; i++) {
        s = "";
        if (divisible_by(i, 3)) {
            s += "Fizz";
        }
        if (divisible_by(i, 5)) {
            s += "Buzz";
        }
        if (s.size() < 1) {
            stringstream ss;
            ss << i;
            s += ss.str();
        }
        std::cout << s << std::endl;
    }
    return 0;
}
view (27 lines, 100 lines of output)
pasted on May 27:
#include <string>

typedef unsigned int uint;

bool divisible_by(uint n, uint denominator) {
    return n % denominator == 0;
}

int main() {
    std::string s = std::string();
    for (uint i = 1; i <= 100; i++) {
        s = "";
        if (divisible_by(i, 3)) {
            s += "Fizz";
        }
        if (divisible_by(i, 5)) {
            s += "Buzz";
        }
        if (s.size() < 1) {
            stringstream ss;
            ss << i;
            s += ss.str();
        }
        std::cout << s << std::endl;
    }
    return 0;
}
view (27 lines, 100 lines of output)


Haskell:
pasted on May 31:
1
2
3
4
5
6
7
8
9
10
11
y = 5 / 0

z = [1..]

fizzBuzz x 
 | (x `mod` 5 == 0) && (x `mod` 3 == 0) = "FizzBuzz"
 | (x `mod` 5 == 0) = "Buzz"
 | (x `mod` 3 == 0) = "Fizz"
 | otherwise = show x

main = mapM (putStrLn . fizzBuzz) [1..100]
view (13 lines, 100 lines of output)
pasted on May 31:
1
2
3
4
5
6
7
8
9
10
11
12
13
main = mapM putStrLn x

y = 5 / 0

z = [1..]

fizzBuzz x 
 | (x `mod` 5 == 0) && (x `mod` 3 == 0) = "FizzBuzz"
 | (x `mod` 5 == 0) = "Buzz"
 | (x `mod` 3 == 0) = "Fizz"
 | otherwise = show x

x = map fizzBuzz [1..100]
view (13 lines, 100 lines of output)


Lua:
pasted on Mar 19:
1
2
3
4
5
6
7
8
9
10
11
for i=1, 100 do
	if i % 3 == 0 and i % 5 == 0 then
		print("Fizzbuzz")
	elseif i % 3 == 0 then
		print("Fizz")
	elseif i % 5 == 0 then
		print("Buzz")
	else
		print(i)
	end
end
view (11 lines, 100 lines of output)
pasted on Mar 2:
1
2
3
4
5
6
7
8
9
10
11
for i=1, 100 do
	if i % 3 == 0 and i % 5 == 0 then
		print("Fizzbuzz")
	elseif i % 3 == 0 then
		print("Fizz")
	elseif i % 5 == 0 then
		print("Buzz")
	else
		print(i)
	end
end
view (11 lines, 100 lines of output)


PHP:
pasted on May 28:
<?php

error_reporting(0);

$i = 0;
while($i++ < 100)
{
   if($i % 3 != 0) 
   {
       echo !$$i = $i; // or just $$i = $i; (without the echo), but I kept it as it was in the original code)
   }
   else
   {
       echo 'Fizz';
   }
   if($i % 5 != 0)
   {
       echo $$i; // echo the counter
   }
   else
   {
       echo 'Buzz';
   }
   echo "\n";
}

?>
view (27 lines, 100 lines of output)
pasted on May 26:
1
<?for($i=1;$i<=100;$i++){$f=$i%3?'':'fizz';$b=$i%5?'':'buzz';$s=$f.$b?$f.$b:$i;echo $s."\n";}
view (1 line, 100 lines of output)


Perl:
pasted on May 26:
1
2
3
4
5
6
7
map{
    (($_%3 or $c .= "fizz") and ($_%5 or $c .= "buzz")) and $c=~s/^$/$_/;
     
    print "$c\n";
    $c="";
    
}(1..100)
view (7 lines, 100 lines of output)
pasted on May 20:
1
2
3
4
5
6
for my $i (1 .. 100) {
        unless ($i % 3) { print "Fizz"; }
        unless ($i % 5) { print "Buzz"; }
        if ( ($i % 3) && ($i % 5) ) {   print $i;       }
        print "\n";
}
view (6 lines, 100 lines of output)


Python:
pasted yesterday:
1
2
3
4
5
6
7
8
9
for i in range(1,101):
    if i % 15 == 0:
        print "FizzBuzz"
    elif i % 5 == 0:
        print "Buzz"
    elif i % 3 == 0:
        print "Fizz"
    else:
        print i
view (10 lines, 100 lines of output)
pasted yesterday:
1
for x in range(1,101):print'Fizz'*(x%3==0)+'Buzz'*(x%5==0)or x
view (1 line, 100 lines of output)


Ruby:
pasted on May 25:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Write a program that prints the numbers from 1 to 100.
# But for multiples of three print “Fizz” instead of the number and for
# the multiples of five print “Buzz”. For numbers which are multiples of
# both three and five print “FizzBuzz.”

def fizz_buzz
  for i in 1..100
    if (i % 3 == 0) && (i % 5 == 0)
      puts "FizzBuzz"
    elsif i % 3 == 0
      puts "Fizz"
    elsif i % 5 == 0
      puts "Buzz"
    else
      puts i
    end
  end
end

fizz_buzz
view (20 lines, 100 lines of output)
pasted on May 25:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Write a program that prints the numbers from 1 to 100.
# But for multiples of three print “Fizz” instead of the number and for
# the multiples of five print “Buzz”. For numbers which are multiples of
# both three and five print “FizzBuzz.”

def fizz_buzz
  for i in 1..100
    if (i % 3 == 0) && (i % 5 == 0)
      puts "FizzBuzz"
    elsif i % 3 == 0
      puts "Fizz"
    elsif i % 5 == 0
      puts "Buzz"
    else
      puts i
    end
  end
end

fizz_buzz
view (20 lines, 100 lines of output)


Scheme:
pasted on Jan 19:
1
2
3
4
5
6
7
8
(do ((i 1 (+ i 1)))
    ((> i 100))
    (display
      (cond ((= 0 (modulo i 15)) "FizzBuzz")
            ((= 0 (modulo i 3))  "Fizz")
            ((= 0 (modulo i 5))  "Buzz")
            (else                i)))
    (newline))
view (8 lines, 100 lines of output)
pasted on Jan 2:
1
2
3
4
5
6
7
8
(do ((i 1 (+ i 1)))
    ((> i 100))
    (display
      (cond ((= 0 (modulo i 15)) "FizzBuzz")
            ((= 0 (modulo i 3))  "Fizz")
            ((= 0 (modulo i 5))  "Buzz")
            (else                i)))
    (newline))
view (8 lines, 100 lines of output)