<html>
<head>
<meta name="description" content="Collatz conjecture">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<p>The <a href="https://en.wikipedia.org/wiki/Collatz_conjecture">Collatz Conjecture</a> is very simple: Start with any positive integer n.
Apply n × 3 + 1 if the number is odd, n ÷ 2 if the number is even.
Repeat the process and you will eventually reach 1.
It's called a conjecture because mathematicians haven't been able
to prove that it is true in all cases.</p>
<button onclick="startCollatz();">Start Collatz</button>
<h3>Number: <span id="starting-number">(loading)</span>, Iterations: <span id="iterations">0</span></h3>
<table>
<thead>
<tr>
<th>Iterations</th>
<th>Number</th>
<th>Is odd?</th>
<th>Formula</th>
<th>Result</th>
</tr>
</thead>
<tbody id="collatz-out">
</tbody>
</table>
<h3>Computation time: <span id="computation-time">(loading)</span></h3>
</body>
</html>
// Bug: still doesn't errror out with large numbers
var collatzOutput = '';
function collatz(num, iterations) {
if (!iterations) iterations = 1;
var startNum = num;
var isOdd = num % 2 !== 0;
var formula = '';
if (num % 2 === 0) {
formula = startNum + ' / 2';
num = num / 2;
} else {
formula = startNum + ' × 3 + 1';
num = num * 3 + 1;
}
collatzOutput = collatzOutput + '<tr><td>'+iterations+'</td><td>'+startNum+'</td><td>'+isOdd+'</td><td>'+formula+'</td><td>'+num+'</td></tr>';
if (num > 1) {
collatz(num, iterations+1);
} else {
t1 = new Date().getTime();
document.getElementById('collatz-out').innerHTML = collatzOutput;
document.getElementById('computation-time').innerText = (t1 - t0) + ' ms';
document.getElementById('iterations').innerText = iterations;
}
}
var t0 = 0;
var t1 = 0;
function startCollatz(num) {
var start = 0;
if (!num) {
start = prompt("Enter any integer greater than 0");
} else {
start = num;
}
if (isInt(start) && start > 0) {
document.getElementById('starting-number').innerText = start;
var tbl = document.getElementById('collatz-out');
/*while(tbl.firstChild.nextSibling) {
alert(tbl.firstChild.nextSibling.innerHTML);
tbl.removeChild(tbl.firstChild.nextSibling);
}*/
tbl.innerHTML = '<tr><td>Running...</td></tr>';
collatzOutput = '';
t0 = new Date().getTime();
collatz(start);
} else {
if (start) alert('Please type in an integer greater than zero.');
}
}
// isInt which also works on large exponent values.
function isInt(num) {
var iNum = num;
num = num + '';
if (num.match(/\./)) {
return num.match(/e/i);
} else {
try {
if (!Number.isSafeInteger(parseInt(iNum))) alert('Number is greater than 2^53 - 1, or 9007199254740991. Behavior may be unpredictable!');
return true;
} catch(e) {
alert('Error. Number probably too big.');
return false;
}
}
}
window.onload = function() {
startCollatz(5);
}
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |