All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fuookami.ospf.kotlin.utils.math.ordinary.LCM.kt Maven / Gradle / Ivy

There is a newer version: 1.0.29
Show newest version
package fuookami.ospf.kotlin.utils.math.ordinary

import fuookami.ospf.kotlin.utils.math.*
import fuookami.ospf.kotlin.utils.operator.*

fun  lcm(x: I, y: I): I where I : Integer, I : Rem, I : Minus, I : Div {
    val px = x.abs()
    val py = y.abs()
    val thisGCD = gcd(px, py)
    return (px / thisGCD) * py
}

fun  lcm(numbers: List): I where I : Integer, I : Rem, I : Div {
    val pn = numbers.map { it.abs() }.sortedDescending()
    val thisGCD = gcdImpl(pn)
    return pn.fold(numbers.first().constants.one) { lhs, rhs ->
        lhs * (rhs / thisGCD)
    } * thisGCD
}

fun  lcm(x: I, y: I, z: I, vararg numbers: I): I where I : Integer, I : Rem, I : Div {
    return lcm(listOf(x, y, z) + numbers.toList())
}