-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathPlus.js
More file actions
149 lines (146 loc) · 3.03 KB
/
mathPlus.js
File metadata and controls
149 lines (146 loc) · 3.03 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const mathjs = require('mathjs')
const mathPlus = {
bigInt: require('big-integer'),
primeFactors (n) {
const factors = []
let p = 2
while (n > 1) {
if (n % p === 0) {
factors.push(p)
n /= p
}
p += 1
}
return factors
},
getPrimes (limit) {
const arr = [2, 3]
let counter = 4
while (arr.length < limit) {
if (counter % 2 !== 0 && counter % 3 !== 0) {
let temp = 4
while (temp * temp <= counter) {
if (counter % temp === 0) {
break
}
temp++
}
if (temp * temp > counter) {
arr.push(counter)
}
}
counter++
}
return arr
},
getPrimesSum (limit) {
let sum = 5 // 2 + 3
let counter = 4 // next prime
while (counter < limit) {
if (counter % 2 !== 0 && counter % 3 !== 0) {
let temp = 4
while (temp * temp <= counter) {
if (counter % temp === 0) {
break
}
temp++
}
if (temp * temp > counter) {
sum += counter
}
}
counter++
}
return sum
},
isPalindrome (s) {
const len = s.length
if (len <= 1) {
return true
}
if (s[0] !== s[len - 1]) {
return false
}
return mathPlus.isPalindrome(s.substring(1, len - 1))
},
pythagoreanTriplet (limit) {
// Returns the pythagorean triplet (a, b, c) where a + b + c = limit
let a = 1
let b = 2
let c = 0
while (true) {
c = limit - a - b
if (c < 3) {
return []
}
if (a * a + b * b === c * c) {
break
}
if (a + 1 < b) {
a += 1
} else {
a = 1
b += 1
}
}
return [a, b, c]
},
nbDivisors (n) {
let nb = 0
let limit = n
let count = 1
while (count < limit) {
if (n % count === 0) {
limit = n / count
nb += 1
}
count += 1
}
return nb * 2
},
divisors (n, isProper = false) {
const divisors = []
for (let d = 1, c = mathjs.sqrt(mathjs.abs(n)); d <= c; d++) {
if (n % d === 0) {
divisors.push(d)
if (d > 1) {
divisors.push(n / d)
}
}
}
if (!isProper) {
divisors.push(n)
}
return divisors
},
collatzSequenceCount (n) {
let count = 1
while (n > 1) {
if (n % 2 === 0) n /= 2
else n = 3 * n + 1
count++
}
return count
},
bigFactorial (n) {
if (n === 0) return mathPlus.bigInt(1)
return mathPlus.bigInt(n).times(mathPlus.bigFactorial(n - 1))
},
isAbundant: n => n < mathPlus.divisors(n, true).reduce((a, v) => a + v, 0)
}
const tools = {
getNumbersArray (file) {
const numbers = []
require('fs')
.readFileSync(`./public/files/${file}`)
.toString()
.split('\n')
.forEach(line => {
if (line !== '') {
numbers.push(line.split(' ').map(item => parseInt(item)))
}
})
return numbers
}
}
module.exports = Object.assign({}, mathjs, mathPlus, tools)