Skip to content

Instantly share code, notes, and snippets.

@alexmelyon
Created August 7, 2019 11:46
Show Gist options
  • Save alexmelyon/9889ebd742ef17d42fcb7807f0d3f45a to your computer and use it in GitHub Desktop.
Save alexmelyon/9889ebd742ef17d42fcb7807f0d3f45a to your computer and use it in GitHub Desktop.
Lcm for array in Kotlin
fun main() {
val list = listOf(24, 36, 145, 48, 72L)
list.reduce { total, next -> lcm(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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment