-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
145 lines (125 loc) · 4.76 KB
/
index.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Collatz conjecture tester</title>
<link rel="stylesheet" href="style.css">
<script>
$ = (q, n) => (n || document).querySelector(q);
$$ = (q, n) => (n || document).querySelectorAll(q);
console.log('hi');
</script>
</head>
<body>
<h1>Collatz conjecture tester</h1>
<p><a href="https://github.com/logico-philosophical/collatz-tester" style="text-decoration: none;"><img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDE2IDE2IiB2ZXJzaW9uPSIxLjEiPjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgZD0iTTggMEMzLjU4IDAgMCAzLjU4IDAgOGMwIDMuNTQgMi4yOSA2LjUzIDUuNDcgNy41OS40LjA3LjU1LS4xNy41NS0uMzggMC0uMTktLjAxLS44Mi0uMDEtMS40OS0yLjAxLjM3LTIuNTMtLjQ5LTIuNjktLjk0LS4wOS0uMjMtLjQ4LS45NC0uODItMS4xMy0uMjgtLjE1LS42OC0uNTItLjAxLS41My42My0uMDEgMS4wOC41OCAxLjIzLjgyLjcyIDEuMjEgMS44Ny44NyAyLjMzLjY2LjA3LS41Mi4yOC0uODcuNTEtMS4wNy0xLjc4LS4yLTMuNjQtLjg5LTMuNjQtMy45NSAwLS44Ny4zMS0xLjU5LjgyLTIuMTUtLjA4LS4yLS4zNi0xLjAyLjA4LTIuMTIgMCAwIC42Ny0uMjEgMi4yLjgyLjY0LS4xOCAxLjMyLS4yNyAyLS4yNy42OCAwIDEuMzYuMDkgMiAuMjcgMS41My0xLjA0IDIuMi0uODIgMi4yLS44Mi40NCAxLjEuMTYgMS45Mi4wOCAyLjEyLjUxLjU2LjgyIDEuMjcuODIgMi4xNSAwIDMuMDctMS44NyAzLjc1LTMuNjUgMy45NS4yOS4yNS41NC43My41NCAxLjQ4IDAgMS4wNy0uMDEgMS45My0uMDEgMi4yIDAgLjIxLjE1LjQ2LjU1LjM4QTguMDEzIDguMDEzIDAgMDAxNiA4YzAtNC40Mi0zLjU4LTgtOC04eiI+PC9wYXRoPjwvc3ZnPg==" style="vertical-align: middle; margin-right:.5em">GitHub repository</a></p>
<p id="bigint-warning" class="warn" style="display:none">Uh-oh, the tester would not work because this browser does not support <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt"><code>BigInt</code></a>. Sorry!</p>
<script>
if (typeof BigInt == 'undefined') {
$('#bigint-warning').style.display = 'block';
}
try {
eval('1n');
} catch (e) {
$('#bigint-warning').style.display = 'block';
}
</script>
<p>Test if the <a href="https://en.wikipedia.org/wiki/Collatz_conjecture">Collatz sequence</a> of an arbitrary positive integer eventually reaches 1.</p>
<div>
<input id="num" type="text" placeholder="Enter a positive integer">
<span id="num-message" style="display:none"></span>
<script>
function setErrorMessage(message) {
if (!message) {
$('#num').classList.remove('error');
$('#num-message').innerText = '';
$('#num-message').style.display = 'none';
} else {
$('#num').classList.add('error');
$('#num-message').innerText = message;
$('#num-message').style.display = 'inline-block';
}
}
</script>
</div>
<div style="margin-top:.5em"><button id="submit">test</button></div>
<h2>Result</h2>
<p><span class="odd">red</span>: odd, <span class="even">green</span>: even</p>
<table id="result"></table>
<script>
function normalizeInput(input) {
return input.replace(/,|\s/g, '');
}
function getErrorMessage(input) {
if (!/^\+?[0-9]+$/.test(input) || /^\+?0+$/.test(input)) {
return 'Enter a positive integer!';
}
return false;
}
function iterate(num) {
if (num % 2n == 0) {
return num / 2n;
} else {
return 3n * num + 1n;
}
}
function printSequence(num) {
var $table = $('#result');
$table.innerHTML = '<tr><th>#</th><th>the number</th></tr>';
var i = 0;
while (true) {
var $tr = document.createElement('tr');
var $td1 = document.createElement('td');
$td1.innerText = i;
$tr.appendChild($td1);
var $td2 = document.createElement('td');
$td2.innerText = num;
$td2.classList.add(num == 1 ? 'one' : num % 2n == 0 ? 'even' : 'odd');
$tr.appendChild($td2);
$table.appendChild($tr);
if (num == 1) break;
i++;
num = iterate(num);
}
}
$('#num').addEventListener('keydown', evt => {
if (evt.key == 'Enter') {
evt.preventDefault();
$('#submit').click();
}
});
$('#num').addEventListener('input', evt => {
var input = normalizeInput($('#num').value);
if (!input.length) {
setErrorMessage(false);
} else {
var message = getErrorMessage(input);
setErrorMessage(message);
}
})
$('#submit').addEventListener('click', () => {
var input = normalizeInput($('#num').value);
var num;
var message = getErrorMessage(input);
setErrorMessage(message);
if (message) {
alert(message);
return;
}
try {
num = BigInt(input);
} catch (e) {
alert('Errored! Check out the console to see the message.');
console.error(e);
return;
}
if (num <= 0) {
alert('How did this happen?');
return;
}
printSequence(num);
});
</script>
</body>
</html>