Skip to content

Instantly share code, notes, and snippets.

@alexmelyon
Created August 7, 2019 11:46
Show Gist options
  • Select an option

  • Save alexmelyon/9889ebd742ef17d42fcb7807f0d3f45a to your computer and use it in GitHub Desktop.

Select an option

Save alexmelyon/9889ebd742ef17d42fcb7807f0d3f45a to your computer and use it in GitHub Desktop.

Revisions

  1. alexmelyon revised this gist Aug 7, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion LcmArray.kt
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@
    fun main() {
    val list = listOf(24, 36, 145, 48, 72L)
    list.reduce { total, next -> lcm(total, next) }.let { println(it) }
    list.reduce { total, next -> total * next }.let { println(it) }
    }

    /**
  2. alexmelyon created this gist Aug 7, 2019.
    20 changes: 20 additions & 0 deletions LcmArray.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@

    fun main() {
    val list = listOf(24, 36, 145, 48, 72L)
    list.reduce { total, next -> lcm(total, next) }.let { println(it) }
    list.reduce { total, next -> total * next }.let { println(it) }
    }

    /**
    * НОК Наименьшее общее кратное
    */
    fun lcm(a: Long, b: Long): Long {
    return a / gcd(a, b) * b
    }

    /**
    * НОД Наибольший общий делитель
    */
    fun gcd(a: Long, b: Long): Long {
    return if (b == 0L) a else gcd(b, a % b)
    }