LoginSignup
0
0

More than 1 year has passed since last update.

Kotlin を Linux で使う方法

Posted at

Ubuntu 21.10 で Kotlin を使う方法です。

こちらから、コンパイラーをダウンロードします。
Kotlin 1.6.10

wget https://github.com/JetBrains/kotlin/releases/download/v1.6.10/kotlin-native-linux-x86_64-1.6.10.tar.gz

解凍

tar xvfz kotlin-native-linux-x86_64-1.6.10.tar.gz

/usr/local に移動

sudo mv kotlin-native-linux-x86_64-1.6.10 /usr/local
cd /usr/local
sudo ln -s kotlin-native-linux-x86_64-1.6.10 kotlin

.bashrc にパスを加える

.bashrc
(省略)
export PATH=$PATH:/usr/local/kotlin/bin

簡単なプログラム (その1)

hello.kt
fun main() {
    println("Hello World!")
    println("こんにちは")
    println("Feb/07/2022")
}
Makefile
hello: hello.kt
    kotlinc-native hello.kt -o hello
clean:
    rm -f hello.kexe

コンパイル

make

実行結果

$ ./hello.kexe 
Hello World!
こんにちは
Feb/07/2022

簡単なプログラム (その2)

pythagoras.kt
// ------------------------------------------------------------------
/*
    pythagoras.kt

                    Feb/07/2022
*/
// ------------------------------------------------------------------
fun display_proc(it:Int, jt:Int, kt:Int)
{
    val tab = '\t'
    var str_out = ""
    str_out += "it = "
    str_out += it.toString() + tab
    str_out += "jt = "
    str_out += jt.toString() + tab
    str_out += "kt = "
    str_out += kt.toString()
    println(str_out)
}

// ------------------------------------------------------------------
fun main() {
    println("*** start ***")

    val nmax = 20
    for (it in 1..nmax)
        {
        var it2 = it * it
        for (jt in it..nmax)
            {
            var jt2 = jt * jt
            var itjt2 = it2 + jt2
            for (kt in jt..nmax)
                {
                var kt2 = kt * kt
                if (itjt2 == kt2)
                    {
                    display_proc(it,jt,kt)
                    }
                }
            }
        }

    println("*** end ***")
}

// ------------------------------------------------------------------
Makefile
pythagoras: pythagoras.kt
    kotlinc-native pythagoras.kt -o pythagoras
clean:
    rm -f pythagoras.kexe

コンパイル

make

実行結果

$ ./pythagoras.kexe 
*** start ***
it = 3  jt = 4  kt = 5
it = 5  jt = 12 kt = 13
it = 6  jt = 8  kt = 10
it = 8  jt = 15 kt = 17
it = 9  jt = 12 kt = 15
it = 12 jt = 16 kt = 20
*** end ***
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