🔢

色々な言語のFizz Buzz問題

2023/09/23に公開

概要

Fizz Buzz問題とは

  • 数値が3の倍数の時:Fizz、5の倍数の時:Buzz、3と5の公倍数の時:FizzBuzzと答えるゲーム

プログラム仕様

  • 数値は 1~30 までとする
  • メイン関数は130をオブジェクトや関数に設定し呼び出すのみとする
  • 数値の判定をする関数(num_check)と、結果表示と繰り返しを行う関数(fizzbuzz)は別とする
  • クラス等のオブジェクト指向の機能は備わっている言語はその機能を使用する

出力結果(1 ~ 30)

  1       
  2       
  3 : Fizz
  4
  5 : Buzz
  6 : Fizz
  7
  8
  9 : Fizz
 10 : Buzz
 11
 12 : Fizz
 13
 14
 15 : Fizz Buzz
 16
 17
 18 : Fizz
 19
 20 : Buzz
 21 : Fizz
 22
 23
 24 : Fizz
 25 : Buzz
 26
 27 : Fizz
 28
 29
 30 : Fizz Buzz

C言語

main.c
#include <stdio.h>

char *num_check(unsigned int n)
{
    char *str;

    if (n % 15 == 0)
    {
        return " : Fizz Buzz";
    }
    else if (n % 5 == 0)
    {
        return " : Buzz";
    }
    else if (n % 3 == 0)
    {
        return " : Fizz";
    }

    return "";
}

void fizzbuzz(unsigned int start, unsigned int end)
{
    unsigned int n = 0;

    for (n = start; n <= end; n++)
    {
        printf("%3d%s\r\n", n, num_check(n));
    }
}

int main(void)
{
    fizzbuzz(1, 30);
    return 0;
}

C++

main.cpp
#include <iostream>
#include <iomanip>

using std::string;

class Fizzbuzz
{
private:
    unsigned int start;
    unsigned int end;

public:
    Fizzbuzz(unsigned int x, unsigned int y)
    {
        start = x;
        end = y;
    }

    void fizzbuzz(void)
    {
        for (unsigned int n = this->start; n <= this->end; n++)
        {
            std::cout << std::setw(3) << n << this->num_check(n) << "\r\n";
        }
    }

    string num_check(unsigned int n)
    {
        if (n % 15 == 0)
        {
            return " : Fizz Buzz";
        }
        else if (n % 5 == 0)
        {
            return " : Buzz";
        }
        else if (n % 3 == 0)
        {
            return " : Fizz";
        }

        return "";
    }
};

int main(void)
{
    Fizzbuzz *foo = new Fizzbuzz(1, 30);
    foo->fizzbuzz();

    return 0;
}

C#

Program.cs
class Fizzbuzz
{
    private uint start;
    private uint end;

    public Fizzbuzz(uint x, uint y)
    {
        start = x;
        end = y;
    }

    public void fizzbuzz()
    {
        for (uint n = this.start; n <= this.end; n++)
        {
            Console.Write("{0}{1}\r\n", n, num_check(n));
        }
    }

    public string num_check(uint n)
    {
        if (n % 15 == 0)
        {
            return " : Fizz Buzz";
        }
        else if (n % 5 == 0)
        {
            return " : Buzz";
        }
        else if (n % 3 == 0)
        {
            return " : Fizz";
        }

        return "";
    }
}

class Program
{
    static void Main(string[] args)
    {
        Fizzbuzz foo = new Fizzbuzz(1, 30);
        foo.fizzbuzz();
    }
}

Java

main.java
class Fizzbuzz {
    private int start;
    private int end;

    public Fizzbuzz(int start, int end) {
        this.start = start;
        this.end = end;
    }

    public void fizzbuzz() {
        for (int n = this.start; n <= this.end; n++) {
            System.out.printf("%3d%s\r\n", n, num_check(n));
        }
    }

    public String num_check(int n) {
        if (n % 15 == 0) {
            return " : Fizz Buzz";
        } else if (n % 5 == 0) {
            return " : Buzz";
        } else if (n % 3 == 0) {
            return " : Fizz";
        }

        return "";
    }
}

public class Main {
    public static void main(String[] args) {
        Fizzbuzz foo = new Fizzbuzz(1, 30);
        foo.fizzbuzz();
    }
}

Rust

main.rs
struct Fizzbuzz {
    start: u8,
    end: u8,
}

impl Fizzbuzz {
    fn fizzbuzz(&self) {
        for n in self.start..=self.end {
            if let Some(str) = Fizzbuzz::num_check(n) {
                println!("{:>3} : {}", n, str);
            } else {
                println!("{:>3}", n);
            }
        }
    }

    fn num_check(x: u8) -> Option<String> {
        match x {
            x if (x % 15) == 0 => Some(String::from("FizzBuzz")),
            x if (x % 3) == 0 => Some(String::from("Fizz")),
            x if (x % 5) == 0 => Some(String::from("Buzz")),
            _ => None,
        }
    }

    fn new(start: u8, end: u8) -> Self {
        Self { start, end }
    }
}

fn main() {
    let foo = Fizzbuzz::new(1, 30);
    foo.fizzbuzz();
}

Go

main.go
package main

import (
    "fmt"
)

type Fizzbuzz struct {
    start uint8
    end uint8
}

func (f *Fizzbuzz) fizzbuzz() {
    for n := f.start; n <= f.end; n++ {
        fmt.Printf("%3d%s\r\n", n, f.num_check(n))
    }
}

func (f *Fizzbuzz) num_check(n uint8) string {
    if n%15 == 0 {
        return " : Fizz Buzz"
    } else if n%5 == 0 {
        return " : Buzz"
    } else if n%3 == 0 {
        return " : Fizz"
    }
    return ""
}

func main() {
    foo := Fizzbuzz{1, 30}
    foo.fizzbuzz()
}

Javascript

main.js
class Fizzbuzz {
  constructor(start, end) {
    this.start = start;
    this.end = end;
  }

  num_check(n) {
    if (n % 15 == 0) {
      return " : Fizz Buzz";
    } else if (n % 5 == 0) {
      return " : Buzz";
    } else if (n % 3 == 0) {
      return " : Fizz";
    }

    return "";
  }

  fizzbuzz() {
    for (let n = this.start; n <= this.end; n++) {
      console.log("%s%s", n.toString().padStart(3, " "), this.num_check(n));
    }
  }
}

const foo = new Fizzbuzz(1, 30);
foo.fizzbuzz();

Typescript

main.ts
class Fizzbuzz {
  start: number;
  end: number;

  constructor(start: number, end: number) {
    this.start = start;
    this.end = end;
  }

  num_check(n: number) {
    if (n % 15 == 0) {
      return " : Fizz Buzz";
    } else if (n % 5 == 0) {
      return " : Buzz";
    } else if (n % 3 == 0) {
      return " : Fizz";
    }

    return "";
  }

  fizzbuzz() {
    for (let n = this.start; n <= this.end; n++) {
      console.log("%s%s", n.toString().padStart(3, " "), this.num_check(n));
    }
  }
}

const foo = new Fizzbuzz(1, 30);
foo.fizzbuzz();

Python

main.py
def num_check(n):

    if n % 15 == 0:
        return " : Fizz Buzz"
    elif n % 5 == 0:
        return " : Buzz"
    elif n % 3 == 0:
        return " : Fizz"
    
    return ""

def fizzbuzz(start, end):
    for n in range(start, end+1, 1):
        print("%3d%s" %(n, num_check(n)))

fizzbuzz(1, 30)

Julia

main.jl
using Printf

function num_check(n)

    if n % 15 == 0
        return " : Fizz Buzz"
    elseif n % 5 == 0
        return " : Buzz"
    elseif n % 3 == 0
        return " : Fizz"
    end

    return ""
end

function fizzbuzz(start, ends)
    for n = start:ends
        @printf("%3d%s\r\n", n, num_check(n))
    end
end

fizzbuzz(1, 30)

PowerShell

main.ps1
function num_check($n) {
        If (($n % 15) -eq 0) {
            return " : Fizz Buzz"
        } ElseIf (($n % 5) -eq 0) {
            return " : Buzz"
        } ElseIf (($n % 3) -eq 0) {
            return " : Fizz"
        }
        return ""
}

function fizzbuzz($start, $end) {
    for ($n = $start; $n -le $end; $n++) {
        $str = num_check $n
        Write-Output("{0,3}{1}" -f $n, $str)
    }
}

fizzbuzz 1 30

Discussion