Category talk:Wren-math: Difference between revisions
Content added Content deleted
(→Source code: Ensure consistency between types.) |
(→Source code: Added methods to calculate gcd and lcm of a list of integers.) |
||
Line 120: | Line 120: | ||
if (x == 0 && y == 0) return 0 |
if (x == 0 && y == 0) return 0 |
||
return (x*y).abs / gcd(x, y) |
return (x*y).abs / gcd(x, y) |
||
} |
} |
||
// Returns the greatest common divisor of a list of integers 'a'. |
|||
static gcd(a) { |
|||
if (!(a is List) || a.count < 2) { |
|||
Fiber.abort("Argument must be a list of at least two integers.") |
|||
} |
|||
var g = gcd(a[0], a[1]) |
|||
if (a.count == 2) return g |
|||
return gcd(a[2..-1] + [g]) |
|||
} |
|||
// Returns the least common multiple of a list of integers 'a'. |
|||
static lcm(a) { |
|||
if (!(a is List) || a.count < 2) { |
|||
Fiber.abort("Argument must be a list of at least two integers.") |
|||
} |
|||
var l = lcm(a[0], a[1]) |
|||
if (a.count == 2) return l |
|||
return lcm(a[2..-1] + [l]) |
|||
} |
|||
// Returns the remainder when 'b' raised to the power 'e' is divided by 'm'. |
// Returns the remainder when 'b' raised to the power 'e' is divided by 'm'. |