[ create a new paste ] login | about

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

C:
pasted on Dec 6:
#include <stdio.h>

int main(int argc, char * argv[]) {
        int i, i_start = 1, i_end = 100;

        unsigned char flag = 0;
        for (i = i_start; i <= i_end; ++i) {
                flag = 0;
                if ((i % 3) == 0) {
                        flag += 1;
                }
                if ((i % 5) == 0) {
                        flag += 2;
                }
                if (flag) {
                        if (flag & 0x01) {
                                printf("Fizz");
                        }
                        if (flag & 0x02) {
                                printf("Buzz");
                        }
                }
                else {
                        printf("%d", i);
                }
                printf("\n");
        }
        char b = getchar();
        return 0;
}
view (30 lines, 100 lines of output)
pasted on Oct 16:
#include <stdio.h>

int main(int argc, char * argv[]) {
        int i, i_start = 1, i_end = 100;

        unsigned char flag = 0;
        for (i = i_start; i <= i_end; ++i) {
                flag = 0;
                if ((i % 3) == 0) {
                        flag += 1;
                }
                if ((i % 5) == 0) {
                        flag += 2;
                }
                if (flag) {
                        if (flag & 0x01) {
                                printf("Fizz");
                        }
                        if (flag & 0x02) {
                                printf("Buzz");
                        }
                }
                else {
                        printf("%d", i);
                }
                printf("\n");
        }
        char b = getchar();
        return 0;
}
view (30 lines, 100 lines of output)


C++:
pasted on Dec 5:
#include<iostream>
using namespace std;

void f1(){

int i;
for (i=1;i<=100;i++){
if(i%3==0){
cout<<"fizz";
}//if

if(i%5==0){
cout<<"buzz";
}//if

if( (i%3!=0) && (i%5!=0)){
cout<<i;
}//if

cout<<"\n";
}//for

}//void

int main(){

f1();

}
view (29 lines, 100 lines of output)
pasted on Nov 28:
#include<iostream>
using namespace std;

void f1(){

int i;
for (i=1;i<=100;i++){
if(i%3==0){
cout<<"fizz";
}//if

if(i%5==0){
cout<<"buzz";
}//if


if( (i%3!=0) && (i%5!=0)){
cout<<i;
}//if

cout<<"\n";
}//for

}//void

int main(){

f1();



}
view (32 lines, 100 lines of output)


Haskell:
pasted on Nov 27:
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 Jun 21:
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)


PHP:
pasted on Sep 8:
1
2
3
4
5
6
7
8
9
10
11
<?php
function fizzbuzz($fizz = 3, $buzz = 5, $total = 100, $echo = true){
    $output = '';
    for($i = 1; $i <= $total; $i++) {
        $output .= ($i % $fizz == 0 && $i % $buzz == 0 ? "FizzBuzz\n" : ($i % $fizz == 0 ? "Fizz\n" : ($i % $buzz == 0 ? "Buzz\n" : ($echo ? $i."\n" : ''))));
    }
    return $output;
}

echo fizzbuzz();
?>
view (11 lines, 100 lines of output)
pasted on Aug 23:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
    $input=100;
    for($i=1; $i<=$input; $i++){
        if($i % 3 == 0 && $i % 5 ==0){
            print("FizzBuzz\n");
        } elseif($i % 3 == 0){
            print("Fizz\n");
        } elseif($i % 5 == 0){
            print("Buzz\n");
        } else {
            print($i."\n");
        }
    }
?>
view (14 lines, 100 lines of output)


Perl:
pasted on Nov 28:
# FizzBuzz by code reference. (Without decision logic!!!)



# First, take a fresh array. Be sure to wash it thoroughly to remove any dirt and residue.
# Next, add in four code references, like so:
    @a=(
        sub {print "FizzBuzz\n"},
        sub {print "Buzz\n"},
        sub {print "Fizz\n"},
        sub {print "$_[0]\n"}
    );

# Set the array aside. 


# Next, take a list of numbers (from 1 to 100)
# Then, take two "<=>" (spaceship operators) and two 0s
# Combine them with each test value to produce two new values (1 or 0 for each new value).
#     The two values are the "multiple of 3 test result" and the "multiple of 5 test result."
# Spice up the "multiple of 5 test result" by multiplying it by 2.

#        Mmmmm. . . Spicy!


# Combine the fresh test results to come up with a new value that is
# specific to the properties of the original value.

# A value that is a multiple of 3 and 5 will get a combined test value result of 0.
# A value that is a multiple of 5 will get a combined test value result of 1.
# A value that is a multiple of 3 will get a combined test value result of 2.
# A value that is not a multiple of 3 and 5 will get a combined test value result of 3.


# Take the piping hot test value result and use it as an index for the array of code references.
# Be sure to pass the original value through the array for the times when the code reference 
# at $a[3] is called!!!

    $a[($_%3<=>0) + (2*($_%5<=>0))]->($_) for (1..100)

# And, would you look at that!? Each code reference prints out
# "FizBuzz," "Buzz," "Fizz," or the original value 
# based on that crunchy-on-the-outside, tender-in-the-middle 
# combined test result!
view (46 lines, 100 lines of output)
pasted on Nov 28:
# FizzBuzz by transforming lists. 

# A list that goes from 1 to 100
    @FB1 = (1..100);

# Make a new list from the previous list. 
# The new list replaces all values that are multiples of 3 & 5 with "FizzBuzz"
    @FB2 = map{!($_%3 or $_%5)?'FizzBuzz': $_}@FB1;

# Make a new list from the previous list.
# The new list replaces all values that are multiples of 3 with "Fizz"
    @FB3 = map{(/\d/ and !($_%3))?'Fizz': $_}@FB2;

# Make a new list from the previous list.
# The new list replaces all values that are multiples of 5 with "Buzz"
    @FB4 = map{(/\d/ and !($_%5))?'Buzz': $_}@FB3;

# Make a new list from the previous list.
# Add a line return to each value in that list
    @FB5 = map{$_."\n"}@FB4;

# print the final list and toast your success with some bubbly.
print @FB5;
view (23 lines, 100 lines of output)


Python:
pasted on Nov 16:
1
2
3
4
5
6
7
8
9
10
purpose = {3:'fizz', 5:'buzz'}
for i in range(1,101):
    if i % 3 == 0 and i % 5 == 0:
        print('fizzbuzz')
    elif i % 3 == 0:
        print('fizz')
    elif i % 5 == 0:
        print('buzz')
    else:
        print(i)
view (10 lines, 100 lines of output)
pasted on Nov 7:
1
2
for i in range(1,101):
 print("FizzBuzz"[i*i%3*4:8--i**4%5] or i)
view (2 lines, 100 lines of output)


Ruby:
pasted on Aug 27:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 def fizz_buzz(numbers)
    numbers.each do |number|
        div_by_3 = number % 3 == 0
        div_by_5 = number % 5 == 0

        if div_by_3 && div_by_5
            puts "FizzBuzz"
        elsif div_by_3 
                puts "Fizz"
            elsif div_by_5
                    puts "Buzz"
                else
                    puts number
            end
        end
end

numbers = (1..100).to_a
fizz_buzz(numbers)
view (19 lines, 100 lines of output)


Scheme:
pasted on Jun 29:
1
2
3
4
5
6
7
8
9
(define (fizzbuzz lower upper)
 (cond ((< upper lower) ())
          ((and (eq? (remainder lower 3) 0) (eq? (remainder lower 5) 0)) 
           (display "fizzbuzz\n") (fizzbuzz (+ lower 1) upper))
          ((eq? (remainder lower 3) 0) (display "fizz\n") (fizzbuzz (+ lower 1) upper))
          ((eq? (remainder lower 5) 0) (display "buzz\n") (fizzbuzz (+ lower 1) upper))
(else (display lower) (display "\n") (fizzbuzz (+ lower 1) upper))))

(fizzbuzz 1 100)
view (9 lines, 100 lines of output)
pasted on Jun 27:
1
2
3
4
5
6
7
8
9
(define (fizzbuzz lower upper)
 (cond ((< upper lower) ())
          ((and (eq? (remainder lower 3) 0) (eq? (remainder lower 5) 0)) 
           (display "fizzbuzz\n") (fizzbuzz (+ lower 1) upper))
          ((eq? (remainder lower 3) 0) (display "fizz\n") (fizzbuzz (+ lower 1) upper))
          ((eq? (remainder lower 5) 0) (display "buzz\n") (fizzbuzz (+ lower 1) upper))
(else (display lower) (display "\n") (fizzbuzz (+ lower 1) upper))))

(fizzbuzz 1 100)
view (9 lines, 100 lines of output)