Compare commits

..

2 Commits

Author SHA1 Message Date
7349e971e5 Coprimes up to N 2025-11-18 16:04:21 +01:00
cedcf4b940 Midpoint Sum 2025-11-17 18:43:13 +01:00
2 changed files with 54 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
const midpointSum = arr => {
const sums = [];
let sum = 0;
sums[-1] = 0;
for (let i = 0; i < arr.length; ++i) {
sums[i] = arr[i] + sums[i-1];
sum += arr[i];
}
for (let i = 1; i < arr.length - 1; ++i) {
if (sums[i-1] === sum - sums[i]) {
return i;
}
}
return null;
};
midpointSum([-10,3,7,8,-6,-13,21]);
midpointSum([0,0,4,0]);
midpointSum([9,0,1,2,3,4]);
midpointSum([4, 1, 7, 9, 3, 9]);

View File

@@ -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]