Repository navigation
Expand file tree
/
Copy pathcollatz-math.js
More file actions
148 lines (130 loc) · 4.82 KB
/
Copy pathcollatz-math.js
File metadata and controls
148 lines (130 loc) · 4.82 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
(function (root, factory) {
const api = factory();
if (typeof module === 'object' && module.exports) {
module.exports = api;
}
root.CollatzMath = api;
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
'use strict';
function assertPositiveSafeInteger(value, name = 'value') {
if (!Number.isSafeInteger(value) || value < 1) {
throw new RangeError(`${name} must be a positive safe integer`);
}
}
function forwardCollatz(value) {
assertPositiveSafeInteger(value);
if (value === 1) return 1;
const next = value % 2 === 0 ? value / 2 : (3 * value) + 1;
if (!Number.isSafeInteger(next)) {
throw new RangeError('Collatz step exceeds JavaScript safe-integer precision');
}
return next;
}
function reversePredecessors(value, options = {}) {
assertPositiveSafeInteger(value);
const pruneRootCycle = options.pruneRootCycle !== false;
const doubled = value * 2;
if (!Number.isSafeInteger(doubled)) {
throw new RangeError('Reverse Collatz step exceeds JavaScript safe-integer precision');
}
const predecessors = [doubled];
if ((value - 1) % 3 === 0) {
const candidate = (value - 1) / 3;
const isPrunedRootEdge = pruneRootCycle && value === 4 && candidate === 1;
if (candidate > 0 && candidate % 2 === 1 && !isPrunedRootEdge) {
predecessors.push(candidate);
}
}
return predecessors;
}
function toMK(value) {
assertPositiveSafeInteger(value);
return {
m: Math.floor(value / 6),
k: value % 6
};
}
function generateReverseTree(rootValue, maxDepth, options = {}) {
assertPositiveSafeInteger(rootValue, 'rootValue');
if (!Number.isInteger(maxDepth) || maxDepth < 0) {
throw new RangeError('maxDepth must be a non-negative integer');
}
const stepsMap = new Map([[rootValue, 0]]);
const parentMap = new Map([[rootValue, null]]);
const queue = [rootValue];
let queueIndex = 0;
while (queueIndex < queue.length) {
const current = queue[queueIndex++];
const currentStep = stepsMap.get(current);
if (currentStep >= maxDepth) continue;
for (const predecessor of reversePredecessors(current, options)) {
if (!stepsMap.has(predecessor)) {
stepsMap.set(predecessor, currentStep + 1);
parentMap.set(predecessor, current);
queue.push(predecessor);
}
}
}
return { stepsMap, parentMap };
}
function levelCounts(rootValue, maxDepth, options = {}) {
const { stepsMap } = generateReverseTree(rootValue, maxDepth, options);
const counts = Array(maxDepth + 1).fill(0);
for (const depth of stepsMap.values()) counts[depth] += 1;
return counts;
}
function largestPrimeFactor(value) {
if (!Number.isSafeInteger(value) || value < 2) {
throw new RangeError('value must be a safe integer greater than or equal to 2');
}
let remaining = value;
let largest = 1;
while (remaining % 2 === 0) {
largest = 2;
remaining /= 2;
}
for (let divisor = 3; divisor * divisor <= remaining; divisor += 2) {
while (remaining % divisor === 0) {
largest = divisor;
remaining /= divisor;
}
}
return Math.max(largest, remaining);
}
function next233(value) {
const factor = largestPrimeFactor(value);
return (factor * 10) + (factor % 10);
}
function find233Cycle(startValue = 233, maxIterations = 10000) {
if (!Number.isInteger(maxIterations) || maxIterations < 1) {
throw new RangeError('maxIterations must be a positive integer');
}
const sequence = [];
const firstIndex = new Map();
let current = startValue;
while (!firstIndex.has(current)) {
if (sequence.length >= maxIterations) {
throw new Error(`No cycle found within ${maxIterations} iterations`);
}
firstIndex.set(current, sequence.length);
sequence.push(current);
current = next233(current);
}
const cycleStart = firstIndex.get(current);
return {
preperiod: sequence.slice(0, cycleStart),
cycle: sequence.slice(cycleStart),
repeatedValue: current
};
}
return {
forwardCollatz,
reversePredecessors,
toMK,
generateReverseTree,
levelCounts,
largestPrimeFactor,
next233,
find233Cycle
};
});