-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc_example_run.js
More file actions
executable file
·52 lines (41 loc) · 963 Bytes
/
Copy pathdoc_example_run.js
File metadata and controls
executable file
·52 lines (41 loc) · 963 Bytes
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
#!/usr/bin/env node
let [, , semiprime, html] = process.argv;
let assert = require('assert');
if (semiprime) {
console.log(factor(Number(semiprime)));
}
function factor(semiprime)
{
function triangle(n)
{
assert(n >= 0);
return n * (n + 1) / 2;
}
function sum(a, b)
{
assert(a >= 0);
assert(b >= 1);
return triangle(a) + triangle(a + b) - triangle(b - 1);
}
assert(semiprime > 0);
let a = Math.floor(Math.sqrt(semiprime)) - 1;
let b = 1;
while (true) {
let s = sum(a, b);
if (s > semiprime) {
++b;
--a;
continue;
}
if (s < semiprime) {
let diff = semiprime - s;
let d = Math.max(1, Math.ceil(diff / (a + 1)));
b += d;
continue;
}
if (s == semiprime) {
return [ a + 1, a + b ];
}
}
return [ 1, semiprime ];
}