0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

15の倍数の場合はFizzBuzz(その数-1)を代わりに表示するFizzBuzz

Posted at

元ネタ

Swift

fbp.playground
func fizzBuzz(num: Int) {
    guard 1 <= num else {
        fatalError("num must be larger than 1")
    }
    
    for i in 1...num {
        switch i {
        case i where i % 15 == 0 :
            fizzBuzz(num: i - 1)
        case i where i % 3 == 0 :
            print("Fizz")
        case i where i % 5 == 0 :
            print("Buzz")
        default:
            print(String(i))
        }
    }
}

fizzBuzz(num: 100)

Kotlin

fbp.kt
import java.lang.Exception

fun Int.fizzBuzz() {
    if (this <= 1) {
        throw Exception("this must be larger than 1")
    }
    for (i in 1..this) {
        when {
            i % 15 == 0 -> (i - 1).fizzBuzz()
            i % 3 == 0 -> println("Fizz")
            i % 5 == 0 -> println("Buzz")
            else -> println(i.toString())
        }
    }
}

fun main() {
    100.fizzBuzz()
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?