Groovy: un peu d’algèbre, le pgcd et le ppmc

13 August 2019

Le PGCD

wiki PGCD ou Plus Grand Commun Diviseur

Le PPMC

wiki PPMC ou Plus Petit Commun Multiple

AlgebraUtils.groovy

package com.cheroliv.misc

import groovy.transform.CompileStatic

@CompileStatic
class AlgebraUtils {

    /**
     * Great common divisor
     * Plus grand commun diviseur(pgcd)
     * Great Common Divisor
     * @param a
     * @param b
     * @return
     */
    static Integer gcd(Integer a, Integer b) {
        !b ? a : gcd(b, a % b)
    }


    /**
     * Least common multiple
     * Plus petit commun multiplicateur(ppmc)
     * @param a
     * @param b
     * @return
     */
    static BigInteger lcm(Integer a, Integer b) {
        (a * b / gcd(a, b)) as BigInteger
    }

    static void main(String... args) {
        Integer a = 96
        Integer b = 28
        println "gcd($a, $b) = ${gcd(a, b)}"
        a = 790
        b = 990
        println "lcm($a, $b) = ${lcm(a, b)}"
    }
}

résultat:

gcd(96, 28) = 4
lcm(790, 990) = 78210