[JAVA] [#FizzBuzz] Write in various languages that "count numbers from 1 to 100 and become stupid only when they are multiples of 3 and numbers with 3"

If I don't write the code regularly, I've come across a situation where I can't even write simple logic code, so I'll take a note of what I learned when I re-studied, including the meaning of rehabilitation. I summarized it as.

0. Description

I will try to implement the most representative story of Mr. Nabeats (Sando Katsura) in the world, "Only when the number is a multiple of 3 and the number with 3 is stupid", in various languages.

** Wikipedia --Sando Katsura # Main story & gag ** https://ja.wikipedia.org/wiki/桂三度#主な持ちネタ&ギャグ

I simply output numbers from 1 to 100 with line breaks, but only when the numbers are multiples of 3 and numbers with 3, the emoticon "٩ (ᐛ) و" is combined with ": (colon)". Output after the number.

Output image


1
2
3 : ٩( ᐛ )و
4
5
6 : ٩( ᐛ )و
7
8
9 : ٩( ᐛ )و
10
11
12 : ٩( ᐛ )و
13 : ٩( ᐛ )و
14
15 : ٩( ᐛ )و
16
17
18 : ٩( ᐛ )و
19
20
21 : ٩( ᐛ )و
22
23 : ٩( ᐛ )و
24 : ٩( ᐛ )و
25
26
27 : ٩( ᐛ )و
28
29
30 : ٩( ᐛ )و
31 : ٩( ᐛ )و
32 : ٩( ᐛ )و
33 : ٩( ᐛ )و
34 : ٩( ᐛ )و
35 : ٩( ᐛ )و
36 : ٩( ᐛ )و
37 : ٩( ᐛ )و
38 : ٩( ᐛ )و
39 : ٩( ᐛ )و
40
41
42 : ٩( ᐛ )و
43 : ٩( ᐛ )و
44
45 : ٩( ᐛ )و
46
47
48 : ٩( ᐛ )و
49
50
51 : ٩( ᐛ )و
52
53 : ٩( ᐛ )و
54 : ٩( ᐛ )و
55
56
57 : ٩( ᐛ )و
58
59
60 : ٩( ᐛ )و
61
62
63 : ٩( ᐛ )و
64
65
66 : ٩( ᐛ )و
67
68
69 : ٩( ᐛ )و
70
71
72 : ٩( ᐛ )و
73 : ٩( ᐛ )و
74
75 : ٩( ᐛ )و
76
77
78 : ٩( ᐛ )و
79
80
81 : ٩( ᐛ )و
82
83 : ٩( ᐛ )و
84 : ٩( ᐛ )و
85
86
87 : ٩( ᐛ )و
88
89
90 : ٩( ᐛ )و
91
92
93 : ٩( ᐛ )و
94
95
96 : ٩( ᐛ )و
97
98
99 : ٩( ᐛ )و
100

1. For Bash

You can use grep to judge" numbers with 3 ", so it's surprisingly simple to write.

For Bash


for i in $(seq 1 100) #Loop through numbers from 1 to 100
do
  if [ $(($i % 3)) == 0 ] || [ $(echo "$i" | grep "3") ] #Be stupid only when it is a multiple of 3 and a number with 3
  then
    echo "${i} : ٩( ᐛ )و"
  else
    echo "$i"  #Other than that, only number output
  fi
done

Run on Wandbox

2. For Java

Judgment of "number with 3" is String.valueOf (i) .indexOf ("3 ")! = -1

For Java


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        //Loop through numbers from 1 to 100
        for(int i=1; i<=100; i++){
            //Be stupid only when it is a multiple of 3 and a number with 3
            if(i%3 == 0 || String.valueOf(i).indexOf("3") != -1 ){ 
                System.out.println(i + " : ٩( ᐛ )و");
            } else {
                System.out.println(i);
            }
        }
    }
}

Run in Wandbox

3. For Scala

I think that you can write in various ways, but I personally tried to write it like Scala.

For Scala


object Main extends App{
    println(1 to 100 map ((n) => { //Generate a map using numbers from 1 to 100 as keys
        var s = n.toString() //Substitute the key converted into a character string as it is for the map value
        if(n % 3 == 0 || s.contains('3')) {s += " : ٩( ᐛ )و"} //Be stupid only when it is a multiple of 3 and a number with 3
        s
    }) reduceLeft ((s,e) => { s + "\n" + e }) ) //Output in order while breaking the map value
}

Run in Wandbox (https://wandbox.org/permlink/WrmO5722LlX7ltYe)

4. For PowerShell

I think that you can write in various ways in the case of PowerShell, but I tried to write it like a function following Scala

For PowerShell


1..100 | 
    %{
        if (($_ % 3 -eq 0) -or ($_ -match "3")) { echo("$_" + " : ٩( ᐛ )و") }
        else { echo("$_") }
    }

5. For Python 3

I think Python is a very easy language to write this kind of processing

For Python 3


# coding: utf-8
for num in range(1, 100+1):
    if num % 3 == 0 or "3" in str(num):
        print("{0} : ٩( ᐛ )و".format(num))
    else:
        print(num)

Run in Wandbox

6. For Ruby

Like Python, Ruby is a language in which this kind of processing is very easy to write.

For Ruby


#!/bin/env ruby
for i in 1..100
  if i % 3 == 0 || i.to_s =~ /3/
    printf( "%d : ٩( ᐛ )و\n", i )
  else
    printf( "%d\n", i )
  end
end

Run in Wandbox

7. For Go

I thought that Golang would have a hard time with type conversion, but I was able to write it unexpectedly simply.

For Go


package main

import (
    "fmt"
    "strconv"
    "strings"
)

func main() {
    //Loop through numbers from 1 to 100
    for i := 1; i <= 100; i++ {
        //Convert int to string
        var s string = strconv.Itoa(i)
        //Be stupid only when it is a multiple of 3 and a number with 3
        if i%3 == 0 || strings.Contains(s, "3") {
            fmt.Println(s + " : ٩( ᐛ )و")
        } else {
            fmt.Println(s)
        }
    }
}

Run in Wandbox


that's all.

If you feel like it, I will try other languages.

Recommended Posts

[#FizzBuzz] Write in various languages that "count numbers from 1 to 100 and become stupid only when they are multiples of 3 and numbers with 3"
10 barrages of drawing with ● or ■ that are likely to appear in training (Java)