From 7349e971e539f263fc0aafa8602830b14e2fb981 Mon Sep 17 00:00:00 2001 From: Grzegorz Kucmierz Date: Tue, 18 Nov 2025 16:04:21 +0100 Subject: [PATCH] Coprimes up to N --- 59e0dbb72a7acc3610000017/index.js | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 59e0dbb72a7acc3610000017/index.js diff --git a/59e0dbb72a7acc3610000017/index.js b/59e0dbb72a7acc3610000017/index.js new file mode 100644 index 0000000..6b3eb78 --- /dev/null +++ b/59e0dbb72a7acc3610000017/index.js @@ -0,0 +1,33 @@ + +const getGCD = ZERO => { + return (a, b) => { + if (a < ZERO) a = -a; + if (b < ZERO) b = -b; + if (b > a) { + [a, b] = [b, a]; + } + while (true) { + if (b === ZERO) return a; + a %= b; + if (a === ZERO) return b; + b %= a; + } + }; +}; + +const gcd = getGCD(0); +const gcdBI = getGCD(0n); + +const coprimes = n => { + return new Array(n).fill(0).map((_, i) => i + 1).filter(i => { + return gcd(n, i) === 1; + }); +}; + +coprimes(2); // -> [1] +coprimes(3); // -> [1, 2] +coprimes(6); // -> [1, 5] +coprimes(10); // -> [1, 3, 7, 9] +coprimes(20); // -> [1, 3, 7, 9, 11, 13, 17, 19] +coprimes(25); // -> [1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23, 24] +coprimes(30); // -> [1, 7, 11, 13, 17, 19, 23, 29]