[ create a new paste ] login | about

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

C:
pasted yesterday:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
{
    int i;

    for (i = 1; i <= 100; i++)
    {
        if (i % 3 == 0) printf ("Fizz");
        if (i % 5 == 0) printf ("Buzz");
        if ((i % 3 != 0) && (i % 5 != 0)) printf ("%d", i);
        printf ("\n");
    }

    return 0;
}
view (16 lines, 100 lines of output)
pasted on Apr 22:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

char* i_to_fizzbuzz(int number)
{
	static char buff[10];
	if(number%15 == 0)	return "FizzBuzz\n";
	if(number%3 == 0)	return "Fizz\n";
	if(number%5 == 0)	return "Buzz\n";
	sprintf(buff, "%d\n", number);
	return buff;
}

void PrintFizzBuzz(FILE* stream, int from, int to)
{
	int i;
	for(i=from; i<=to; ++i) fprintf(stream, i_to_fizzbuzz(i)); //オブジェクトiに対してfizzbuzzに変換する機能を追加するイメージ
}

int main(void)
{
	assert((strcmp(i_to_fizzbuzz(0), "FizzBuzz\n") == 0));
	assert((strcmp(i_to_fizzbuzz(1), "1\n") == 0));
	assert((strcmp(i_to_fizzbuzz(2), "2\n") == 0));
	assert((strcmp(i_to_fizzbuzz(3), "Fizz\n") == 0));
	assert((strcmp(i_to_fizzbuzz(4), "4\n") == 0));
	assert((strcmp(i_to_fizzbuzz(5), "Buzz\n") == 0));
	assert((strcmp(i_to_fizzbuzz(6), "Fizz\n") == 0));
	assert((strcmp(i_to_fizzbuzz(7), "7\n") == 0));
	assert((strcmp(i_to_fizzbuzz(9), "Fizz\n") == 0));
	assert((strcmp(i_to_fizzbuzz(10), "Buzz\n") == 0));
	assert((strcmp(i_to_fizzbuzz(11), "11\n") == 0));
	assert((strcmp(i_to_fizzbuzz(14), "14\n") == 0));
	assert((strcmp(i_to_fizzbuzz(15), "FizzBuzz\n") == 0));
	assert((strcmp(i_to_fizzbuzz(16), "16\n") == 0));
	assert((strcmp(i_to_fizzbuzz(30), "FizzBuzz\n") == 0));

	PrintFizzBuzz(stdout, 1, 100);
	return 0;
}
view (42 lines, 100 lines of output)


C++:
pasted on Apr 22:
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int main(void)
{
	for(int i=1; i<=100; ++i)
	{
		if(i%15 == 0) printf("FizzBuzz\n");
		else if(i%3 == 0) printf("Fizz\n");
		else if(i%5 == 0) printf("Buzz\n");
		else printf("%d\n", i);
	}
	return 0;
}
view (12 lines, 100 lines of output)
pasted on Apr 19:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main(int argc, char *argv[]) {
    int i;

    for(i = 1; i <= 100; i++) {
        if(i % (3*5) == 0) printf("FizzBuzz\n");
        else if(i % 5 == 0) printf("Buzz\n");
        else if(i % 3 == 0) printf("Fizz\n");

        /* default */
        else printf("%d\n", i);
    }

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


D:
pasted on Nov 16:
1
2
3
4
5
6
7
8
9
10
import std.stdio;

void main() {
    for (int i = 1; i <= 100; i++) {
        if (i % 3 == 0) writef("Fizz");
        if (i % 5 == 0) writef("Buzz");
        else if(i % 3 != 0) writef("%d", i);
        writef("\n");
    }
}
view (10 lines, 100 lines of output)
pasted on Nov 16:
1
2
3
4
5
6
7
8
9
10
import std.stdio;

void main() {
    for (int i = 1; i <= 100; i++) {
        if (i % 3 == 0) writef("Fizz");
        if (i % 5 == 0) writef("Buzz");
        if (i % 3 != 0 && i % 5 != 0) writef("%d", i);
        writef("\n");
    }
}
view (10 lines, 100 lines of output)


Haskell:
pasted on Apr 7:
1
2
3
4
5
6
7
fb i
 | i `mod` 3 == 0 && i `mod` 5 == 0 = "FizzBuzz"
 | i `mod` 3 == 0 = "Fizz"
 | i `mod` 5 == 0 = "Buzz"
 | otherwise      = show i

main = mapM_ (putStrLn . fb) [1..100] 
view (7 lines, 100 lines of output)
pasted on Apr 7:
1
2
3
4
5
6
7
fb i
 | i `mod` 3 == 0 && i `mod` 5 == 0 = "FizzBuzz"
 | i `mod` 3 == 0 = "Fizz"
 | i `mod` 5 == 0 = "Buzz"
 | otherwise      = show i

main = mapM_ (putStrLn . fb) [1..100] 
view (7 lines, 100 lines of output)


Lua:
pasted on Feb 21:
1
for i=1,100 do print(i%15<1 and"FizzBuzz"or i%3<1 and"Fizz"or i%5<1 and"Buzz"or i)end
view (1 line, 100 lines of output)
pasted on Feb 12:
1
for i=1,1e2 do print(i%3<1 and(i%5<1 and'FizzBuzz'or'Fizz')or i%5<1 and'Buzz'or i)end
view (1 line, 100 lines of output)


PHP:
pasted 6 hours ago:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

for ( $i = 1; $i <= 100; $i++ ) {
    if ( $i % 15 == 0 ) {
        print 'FizzBuzz';
    } else if ( $i % 3 == 0 ) {
        print 'Fizz';
    } else if ( $i % 5 == 0 ) {
        print 'Buzz';
    } else {
        print $i;
    }
    print "\n";
}

?>
view (16 lines, 100 lines of output)
pasted on Apr 15:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
for ( $i = 1; $i <= 100; $i++ ) {
    if ( $i % 15 == 0 ) {
        print 'FizzBuzz';
    } else if ( $i % 3 == 0 ) {
        print 'Fizz';
    } else if ( $i % 5 == 0 ) {
        print 'Buzz';
    } else {
        print $i;
    }
    print "\n";
}
// <プークスクス
?>
view (15 lines, 100 lines of output)


Perl:
pasted on Apr 19:
1
2
3
4
5
6
for (1..100) {
	print "Fizz" if (not $_%3);
	print "Buzz" if (not $_%5);
	print if ($_%5 && $_%3);
	print "\n";
}
view (6 lines, 100 lines of output)
pasted on Mar 26:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
foreach my $i (1..100)
{
    my $result = "";

    if ($i%3 == 0)
    {    
        $result = "Fizz";
    }
    if ($i%5 == 0)
    {
        $result .= "Buzz";
    }
    if (length($result) == 0) { $result = $i;}
    print "$result\n";
    
}
view (16 lines, 100 lines of output)


Python:
pasted yesterday:
1
2
3
4
5
for i in xrange(1,101):
    msg = ""
    if i%3 == 0: msg += "Fizz"
    if i%5 == 0: msg += "Buzz"
    print msg or i
view (5 lines, 100 lines of output)
pasted yesterday:
1
for i in xrange(1,101): print "Fizz"*(i%3==0) + "Buzz"*(i%5==0) or i
view (1 line, 100 lines of output)


Ruby:
pasted on Apr 22:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
module FizzBuzz
	def to_fizzbuzz
		if(self%15 == 0) then
			"FizzBuzz"
		elsif(self%3 == 0) then
			"Fizz"
		elsif(self%5 == 0) then
			"Buzz"
		else
			self.to_i
		end
	end
end

class Integer
	include FizzBuzz
end

(1..100).each {|i| puts(i.to_fizzbuzz)}
view (19 lines, 100 lines of output)
pasted on Mar 28:
1
puts (0..99).map {|i| srand(46308667) if (i%15).zero?; ["FizzBuzz", "Buzz", i+1, "Fizz"][rand(4)]}
view (1 line, 100 lines of output)