Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Kotlinでフィボナッチ数列とfizzbuzzを書いてみた

More than 1 year has passed since last update.

フィボナッチ数

fun fib(n: Int): Int {
    return if (n < 2) n else fib(n -2) + fib(n-1)
}


fun main(args: Array<String>) {
    for (i in 0..10) {
        println(fib(i))
    }
}

実行結果

$ kotlinc fib.kt -include-runtime -d fib.jar
$ kotlin fib.jar
0
1
1
2
3
5
8
13
21
34
55

FizzBuzz

fun fizzbuzz() {
    for (i in 1..30) {
        when {
            (i % 15 == 0) -> println("FizzBuzz")
            (i % 3 == 0) -> println("Fizz")
            (i % 5 == 0) -> println("Buzz")
            else  -> println(i)
        }
    }
}

fun main(args: Array<String>) {
   fizzbuzz()
}

実行結果

$ kotlinc fizzbuzz.kt -include-runtime -d fizzbuzz.jar
$ kotlin fizzbuzz.jar
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
sassy_watson
Androidアプリやフロントエンドのエンジニア。
http://sassy.github.io/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away