-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
118 lines (108 loc) · 2.31 KB
/
utils.js
File metadata and controls
118 lines (108 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const findPrimeFactors = (pnum) => {
let num = pnum;
const primeFactors = [];
while (num % 2 === 0) {
primeFactors.push(2);
num /= 2;
}
const sqrtNum = Math.sqrt(num);
for (let i = 3; i <= sqrtNum; i += 1) {
while (num % i === 0) {
primeFactors.push(i);
num /= i;
}
}
if (num > 2) {
primeFactors.push(num);
}
return primeFactors;
};
const isPrime = (n) => {
if (n <= 1) {
return false;
}
if (n <= 3) {
return true;
}
if (n % 2 === 0 || n % 3 === 0) {
return false;
}
let i = 5;
while (i * i <= n) {
if (n % i === 0 || n % (i + 2) === 0) {
return false;
}
i += 6;
}
return true;
};
const listDivisors = (n) => {
const small = [];
const large = [];
const end = Math.floor(Math.sqrt(n));
for (let i = 1; i <= end; i += 1) {
if (n % i === 0) {
small.push(i);
if (i ** 2 !== n) {
large.push(n / i);
}
}
}
large.reverse();
return small.concat(large);
};
const listProperDivisors = (n) => {
const divisors = listDivisors(n);
divisors.pop();
return divisors;
};
const factorialize = (num) => {
if (num < 0) return -1;
if (num === 0) return 1;
return num * factorialize(num - 1);
};
const combinations = (str) => {
const combin = [];
const recursive = (done, remaining) => {
if (remaining.length === 0) {
combin.push(done);
} else {
for (let i = 0; i < remaining.length; i += 1) {
recursive(
[...done, remaining[i]],
[...remaining.slice(0, i), ...remaining.slice(i + 1)],
);
}
}
};
recursive([], [...str]);
return combin;
};
const quadratic = (a, b, c) => {
const d = b ** 2 - 4 * a * c;
if (d < 0) {
return [];
}
if (d === 0) {
return [(-1 * b) / (2 * a)];
}
return [(-1 * b - Math.sqrt(d)) / (2 * a), (-1 * b + Math.sqrt(d)) / (2 * a)];
};
const wordToNumber = (word) => [...word].map((letter) => letter.charCodeAt(0) - 64).reduce((a, b) => a + b);
const gcd = (a, b) => (b ? gcd(b, a % b) : a);
const reduceDivision = ({num, denom}) => {
gcdVal = gcd(num, denom);
return {num: num / gcdVal, denom: denom / gcdVal};
};
module.exports = {
findPrimeFactors,
isPrime,
listDivisors,
listProperDivisors,
factorialize,
combinations,
quadratic,
wordToNumber,
gcd,
reduceDivision,
};