Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<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 &times; 3 + 1 if the number is odd, n &div; 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 + ' &times; 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

Dismiss x
public
Bin info
sirkoikpro
0viewers