diff --git a/modules/standard/Math.chpl b/modules/standard/Math.chpl index 000f94885c37..f06cd90be12d 100644 --- a/modules/standard/Math.chpl +++ b/modules/standard/Math.chpl @@ -1438,6 +1438,54 @@ module Math { return x; } + /* Returns the least common multiple of the integer arguments `x` + and `y`. */ + proc lcm(x: int, y: int): int { + return x / gcd(x,y) * y; + } + + /* Returns the least common multiple of the integer arguments `x` + and `y`. */ + proc lcm(x: int(32), y: int(32)): int(32) { + return x / gcd(x,y) * y; + } + + /* Returns the least common multiple of the integer arguments `x` + and `y`. */ + proc lcm(x: int(16), y: int(16)): int(16) { + return x / gcd(x,y) * y; + } + + /* Returns the least common multiple of the integer arguments `x` + and `y`. */ + proc lcm(x: int(8), y: int(8)): int(8) { + return x / gcd(x,y) * y; + } + + /* Returns the least common multiple of the unsigned integer arguments `x` + and `y`. */ + proc lcm(x: uint, y: uint): uint { + return x / gcd(x,y) * y; + } + + /* Returns the least common multiple of the unsigned integer arguments `x` + and `y`. */ + proc lcm(x: uint(32), y: uint(32)): uint(32) { + return x / gcd(x,y) * y; + } + + /* Returns the least common multiple of the unsigned integer arguments `x` + and `y`. */ + proc lcm(x: uint(16), y: uint(16)): uint(16) { + return x / gcd(x,y) * y; + } + + /* Returns the least common multiple of the unsigned integer arguments `x` + and `y`. */ + proc lcm(x: uint(8), y: uint(8)): uint(8) { + return x / gcd(x,y) * y; + } + /* Returns the Bessel function of the first kind of order `0` of `x`. */ @unstable("'j0' is unstable and may be renamed or moved to a different module in the future") inline proc j0(x: real(32)): real(32) { diff --git a/test/library/standard/Math/testLCM-int8.good b/test/library/standard/Math/testLCM-int8.good new file mode 100644 index 000000000000..8856ec535c22 --- /dev/null +++ b/test/library/standard/Math/testLCM-int8.good @@ -0,0 +1,22 @@ + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 +----------------------------------------------------------------------------------- + 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 2: 2 2 6 4 10 6 14 8 18 10 22 12 26 14 30 16 34 18 38 20 + 3: 3 6 3 12 15 6 21 24 9 30 33 12 39 42 15 48 51 18 57 60 + 4: 4 4 12 4 20 12 28 8 36 20 44 12 52 28 60 16 68 36 76 20 + 5: 5 10 15 20 5 30 35 40 45 10 55 60 65 70 15 80 85 90 95 20 + 6: 6 6 6 12 30 6 42 24 18 30 66 12 78 42 30 48 102 18 114 60 + 7: 7 14 21 28 35 42 7 56 63 70 77 84 91 14 105 112 119 126 -123 -116 + 8: 8 8 24 8 40 24 56 8 72 40 88 24 104 56 120 16 -120 72 -104 40 + 9: 9 18 9 36 45 18 63 72 9 90 99 36 117 126 45 -112 -103 18 -85 -76 +10: 10 10 30 20 10 30 70 40 90 10 110 60 -126 70 30 80 -86 90 -66 20 +11: 11 22 33 44 55 66 77 88 99 110 11 -124 -113 -102 -91 -80 -69 -58 -47 -36 +12: 12 12 12 12 60 12 84 24 36 60 -124 12 -100 84 60 48 -52 36 -28 60 +13: 13 26 39 52 65 78 91 104 117 -126 -113 -100 13 -74 -61 -48 -35 -22 -9 4 +14: 14 14 42 28 70 42 14 56 126 70 -102 84 -74 14 -46 112 -18 126 10 -116 +15: 15 30 15 60 15 30 105 120 45 30 -91 60 -61 -46 15 -16 -1 90 29 60 +16: 16 16 48 16 80 48 112 16 -112 80 -80 48 -48 112 -16 16 16 -112 48 80 +17: 17 34 51 68 85 102 119 -120 -103 -86 -69 -52 -35 -18 -1 16 17 50 67 84 +18: 18 18 18 36 90 18 126 72 18 90 -58 36 -22 126 90 -112 50 18 86 -76 +19: 19 38 57 76 95 114 -123 -104 -85 -66 -47 -28 -9 10 29 48 67 86 19 124 +20: 20 20 60 20 20 60 -116 40 -76 20 -36 60 4 -116 60 80 84 -76 124 20 diff --git a/test/library/standard/Math/testLCM-uint8.good b/test/library/standard/Math/testLCM-uint8.good new file mode 100644 index 000000000000..2360f809aa61 --- /dev/null +++ b/test/library/standard/Math/testLCM-uint8.good @@ -0,0 +1,22 @@ + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 +----------------------------------------------------------------------------------- + 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 2: 2 2 6 4 10 6 14 8 18 10 22 12 26 14 30 16 34 18 38 20 + 3: 3 6 3 12 15 6 21 24 9 30 33 12 39 42 15 48 51 18 57 60 + 4: 4 4 12 4 20 12 28 8 36 20 44 12 52 28 60 16 68 36 76 20 + 5: 5 10 15 20 5 30 35 40 45 10 55 60 65 70 15 80 85 90 95 20 + 6: 6 6 6 12 30 6 42 24 18 30 66 12 78 42 30 48 102 18 114 60 + 7: 7 14 21 28 35 42 7 56 63 70 77 84 91 14 105 112 119 126 133 140 + 8: 8 8 24 8 40 24 56 8 72 40 88 24 104 56 120 16 136 72 152 40 + 9: 9 18 9 36 45 18 63 72 9 90 99 36 117 126 45 144 153 18 171 180 +10: 10 10 30 20 10 30 70 40 90 10 110 60 130 70 30 80 170 90 190 20 +11: 11 22 33 44 55 66 77 88 99 110 11 132 143 154 165 176 187 198 209 220 +12: 12 12 12 12 60 12 84 24 36 60 132 12 156 84 60 48 204 36 228 60 +13: 13 26 39 52 65 78 91 104 117 130 143 156 13 182 195 208 221 234 247 4 +14: 14 14 42 28 70 42 14 56 126 70 154 84 182 14 210 112 238 126 10 140 +15: 15 30 15 60 15 30 105 120 45 30 165 60 195 210 15 240 255 90 29 60 +16: 16 16 48 16 80 48 112 16 144 80 176 48 208 112 240 16 16 144 48 80 +17: 17 34 51 68 85 102 119 136 153 170 187 204 221 238 255 16 17 50 67 84 +18: 18 18 18 36 90 18 126 72 18 90 198 36 234 126 90 144 50 18 86 180 +19: 19 38 57 76 95 114 133 152 171 190 209 228 247 10 29 48 67 86 19 124 +20: 20 20 60 20 20 60 140 40 180 20 220 60 4 140 60 80 84 180 124 20 diff --git a/test/library/standard/Math/testLCM.chpl b/test/library/standard/Math/testLCM.chpl new file mode 100644 index 000000000000..15b784264bd4 --- /dev/null +++ b/test/library/standard/Math/testLCM.chpl @@ -0,0 +1,42 @@ +use Math; + +config const signed = true, + size = 8; + +proc testLCM(type t, x:t) { + write(" "); + for j in 1..x do + writef("%3i ", j); + writeln(); + + write("---"); + for j in 1..x do + write("----"); + writeln(); + + for i in 1..x { + writef("%2i: ", i); + for j in 1..x { + const res = lcm(i,j); + writef(if signed then "%3i " else "%3u ", res); + } + writeln(); + } +} + +if signed then + select size { + when 8 do testLCM(int(8), 20); + when 16 do testLCM(int(16), 20); + when 32 do testLCM(int(32), 20); + when 64 do testLCM(int(64), 20); + } +else + select size { + when 8 do testLCM(uint(8), 20); + when 16 do testLCM(uint(16), 20); + when 32 do testLCM(uint(32), 20); + when 64 do testLCM(uint(64), 20); + } + + diff --git a/test/library/standard/Math/testLCM.execopts b/test/library/standard/Math/testLCM.execopts new file mode 100644 index 000000000000..bd91b5ed6716 --- /dev/null +++ b/test/library/standard/Math/testLCM.execopts @@ -0,0 +1,8 @@ +--signed=true --size=8 # testLCM-int8.good +--signed=true --size=16 +--signed=true --size=32 +--signed=true --size=64 +--signed=false --size=8 # testLCM-uint8.good +--signed=false --size=16 +--signed=false --size=32 +--signed=false --size=64 diff --git a/test/library/standard/Math/testLCM.good b/test/library/standard/Math/testLCM.good new file mode 100644 index 000000000000..55738c592a35 --- /dev/null +++ b/test/library/standard/Math/testLCM.good @@ -0,0 +1,22 @@ + 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 +----------------------------------------------------------------------------------- + 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 + 2: 2 2 6 4 10 6 14 8 18 10 22 12 26 14 30 16 34 18 38 20 + 3: 3 6 3 12 15 6 21 24 9 30 33 12 39 42 15 48 51 18 57 60 + 4: 4 4 12 4 20 12 28 8 36 20 44 12 52 28 60 16 68 36 76 20 + 5: 5 10 15 20 5 30 35 40 45 10 55 60 65 70 15 80 85 90 95 20 + 6: 6 6 6 12 30 6 42 24 18 30 66 12 78 42 30 48 102 18 114 60 + 7: 7 14 21 28 35 42 7 56 63 70 77 84 91 14 105 112 119 126 133 140 + 8: 8 8 24 8 40 24 56 8 72 40 88 24 104 56 120 16 136 72 152 40 + 9: 9 18 9 36 45 18 63 72 9 90 99 36 117 126 45 144 153 18 171 180 +10: 10 10 30 20 10 30 70 40 90 10 110 60 130 70 30 80 170 90 190 20 +11: 11 22 33 44 55 66 77 88 99 110 11 132 143 154 165 176 187 198 209 220 +12: 12 12 12 12 60 12 84 24 36 60 132 12 156 84 60 48 204 36 228 60 +13: 13 26 39 52 65 78 91 104 117 130 143 156 13 182 195 208 221 234 247 260 +14: 14 14 42 28 70 42 14 56 126 70 154 84 182 14 210 112 238 126 266 140 +15: 15 30 15 60 15 30 105 120 45 30 165 60 195 210 15 240 255 90 285 60 +16: 16 16 48 16 80 48 112 16 144 80 176 48 208 112 240 16 272 144 304 80 +17: 17 34 51 68 85 102 119 136 153 170 187 204 221 238 255 272 17 306 323 340 +18: 18 18 18 36 90 18 126 72 18 90 198 36 234 126 90 144 306 18 342 180 +19: 19 38 57 76 95 114 133 152 171 190 209 228 247 266 285 304 323 342 19 380 +20: 20 20 60 20 20 60 140 40 180 20 220 60 260 140 60 80 340 180 380 20