Compare commits
2 Commits
b36804e8c8
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 7349e971e5 | |||
| cedcf4b940 |
21
54d3bb4dfc75996c1c000c6d/index.js
Normal file
21
54d3bb4dfc75996c1c000c6d/index.js
Normal 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]);
|
||||||
33
59e0dbb72a7acc3610000017/index.js
Normal file
33
59e0dbb72a7acc3610000017/index.js
Normal 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]
|
||||||
Reference in New Issue
Block a user