# mathkit: number helpers that are not built in. # Pure: it needs no permissions, so it can be installed with no `allow` line. # # use mathkit from "cresset_packages/mathkit.crs" # print(mathkit.gcd(48, 18), mathkit.is_prime(97)) # 6 true fn gcd(a, b): x = abs(a) y = abs(b) while y != 0: t = y y = x % y x = t return x fn lcm(a, b): if a == 0 or b == 0: return 0 return abs(a * b) / gcd(a, b) fn is_prime(n): if n < 2 or n != floor(n): return false if n < 4: return true if n % 2 == 0: return false i = 3 while i * i <= n: if n % i == 0: return false i += 2 return true # the prime factors in order, with repeats: prime_factors(12) is [2, 2, 3] fn prime_factors(n): out = [] if n < 2: return out left = n p = 2 while p * p <= left: while left % p == 0: out.push(p) left = left / p p += 1 if left > 1: out.push(left) return out fn factorial(n): if n < 0 or n != floor(n): return err("factorial needs a whole number of 0 or more, not {n}") total = 1 for i in range(2, n + 1): total *= i return ok(total) # n things taken k at a time, in any order fn choose(n, k): if k < 0 or k > n: return 0 smaller = k if n - k < smaller: smaller = n - k total = 1 for i in range(1, smaller + 1): total = total * (n - smaller + i) / i return total # the first `count` Fibonacci numbers fn fibonacci(count): out = [] a = 0 b = 1 for i in range(count): out.push(a) t = a + b a = b b = t return out # keep a number inside [low, high] fn clamp_to(x, low, high): if x < low: return low if x > high: return high return x # the point `t` of the way from a to b: lerp(0, 10, 0.25) is 2.5 fn lerp(a, b, t): return a + (b - a) * t fn sign(x): if x > 0: return 1 if x < 0: return -1 return 0 # the digits of a whole number: digits(407) is [4, 0, 7] fn digits(n): out = [] for ch in str(abs(n)).chars(): out.push(to_int(ch).unwrap()) return out test "gcd and lcm": assert_eq(gcd(48, 18), 6) assert_eq(gcd(-12, 18), 6) assert_eq(gcd(7, 0), 7) assert_eq(lcm(4, 6), 12) assert_eq(lcm(0, 5), 0) test "primes": assert_eq([2, 3, 4, 5, 9, 97, 100].map(n -> is_prime(n)), [true, true, false, true, false, true, false]) assert_eq(is_prime(1), false) assert_eq(is_prime(-7), false) assert_eq(is_prime(2.5), false) assert_eq(prime_factors(12), [2, 2, 3]) assert_eq(prime_factors(97), [97]) assert_eq(prime_factors(360), [2, 2, 2, 3, 3, 5]) assert_eq(prime_factors(1), []) test "factorials and combinations": assert_eq(factorial(0), ok(1)) assert_eq(factorial(5), ok(120)) assert_eq(factorial(-1).is_err(), true) assert_eq(factorial(2.5).is_err(), true) assert_eq(choose(5, 2), 10) assert_eq(choose(10, 0), 1) assert_eq(choose(10, 10), 1) assert_eq(choose(52, 5), 2598960) assert_eq(choose(3, 5), 0) test "sequences": assert_eq(fibonacci(8), [0, 1, 1, 2, 3, 5, 8, 13]) assert_eq(fibonacci(0), []) assert_eq(digits(407), [4, 0, 7]) assert_eq(digits(-90), [9, 0]) test "clamping and blending": assert_eq([clamp_to(5, 0, 10), clamp_to(-3, 0, 10), clamp_to(99, 0, 10)], [5, 0, 10]) assert_eq(lerp(0, 10, 0.25), 2.5) assert_eq(lerp(10, 20, 0), 10) assert_eq([sign(-4), sign(0), sign(9)], [-1, 0, 1])