package kapitel_01 /** * Beispiel aus * * - Algorithmen und Datenstrukturen für Dummies * - von Andreas Gogol-Döring und Thomas Letschert * - Verlag Wiley-VCH; Oktober 2019 * - Kapitel 1, Das sind Algorithmen * * @author A. Gogol-Döring, Th. Letschert */ object AuD_01_02_GGT_App extends App { def GGT1(m: Int, n: Int): Int = { val t = ( for (x <- 1 to 10000//Int.MaxValue; if (m % x) == 0 && (n % x) == 0 ) yield x ) . max return t } println(GGT1(12, 18)) def GGT2(m: Int, n: Int): Int = { var t = 1 for (x <- 1 to Math.min(m, n)) if ((m % x) == 0 && (n % x) == 0) t = x t } println(GGT2(12, 18)) def GGT2math1(m: Int, n: Int): Int = { val g = Math.min(m, n) val t = (for (x <- 1 to g; if (m % x) == 0 && (n % x) == 0 ) yield x) . max t } println(GGT2math1(12, 18)) def GGT2math2(m: Int, n: Int): Int = { val g = Math.min(m, n) val t = (1 to g).filter(x => (m % x) == 0 && (n % x) == 0).max t } println(GGT2math2(12, 18)) def GGT2math3(m: Int, n: Int): Int = (1 to Math.min(m, n)) .filter(x => (m % x) == 0 && (n % x) == 0) .max println(GGT2math3(12, 18)) def GGT3(m: Int, n: Int): Int = { def isDivisor(x: Int, z:Int) : Boolean = { var z_t = z while (z_t > 0) z_t = z_t - x z_t == 0 } var t = 1 for (x <- 2 to Math.min(n, m)) if (isDivisor(x, m) && isDivisor(x, n)) t = x t } println(GGT3(12, 18)) }