Collatz Conjecture

Enter initial value


The Collatz Conjecture posits that given the following steps:
  1. Start with any integer (whole number).
    1. If it's even, divide by 2.
    2. If it's odd, multiply by 3 and add 1.
  2. Repeat step 2.
you will eventually reach 1 with any starting integer.

So this page lets you try it out!

<?php
$ct = file_get_contents('collatz.php');
$ct = filter_var(trim($ct),FILTER_SANITIZE_SPECIAL_CHARS);
$ct = preg_replace('/&#10;/','<br>',$ct);
$ct = preg_replace('/&#9;/','&nbsp;&nbsp;&nbsp;&nbsp;',$ct);
$ct = preg_replace('/"/','\"',$ct);


$collatz_out = "";
if(isset($_POST['collatz'])) {
    $collatz = intval($_POST['collatz']);
    if($collatz == 0) {
        $collatz_out = "That was not a value I can accept.";
    } elseif($collatz == 1) {
        $collatz_out = "1 is already the endpoint.";
    } else {
        $collatz_out = "<h3>Result</h3>";
        $coliter = $collatz;
        $coeven = $coliter%2==0 ? "even" : "odd";
        $collatz_out .= "<strong>Initial: $coliter</strong><br>\n";
        while($coliter != 1) {
            $cooper = $coliter%2==0 ? "$coliter/2 =" : "($coliter*3)+1 =";
            $coliter = $coliter%2==0 ? $coliter/2 : ($coliter*3)+1;
            $coeven = $coliter%2==0 ? "even" : "odd";
            //$collatz_out .= "$cooper $coliter ($coeven)<br>\n";
            $collatz_out .= "$coliter<br>\n";
        }
    }
}    
?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Collatz Conjecture</title>
</head>
<body onLoad="document.getElementById('collatz').focus();">
<div id="leftsize" style="width: 49%; float: left; padding-right: 10px;">
<h2>Collatz Conjecture</h2>
<div style="float: left; width: 49%;">
<form action="" method="POST">
<input type="text" id="collatz" name="collatz" size="5"> Enter initial value<br>
<br>
<input type="submit">
</form>
<br>
The Collatz Conjecture posits that given the following steps:
<ol><li>Start with any integer (whole number).</li>
<li> <ol type="a"><li>If it's even, divide by 2.</li><li>If it's odd, multiply by 3 and add 1.</li></ol></li>
<li> Repeat step 2.</li></ol>

you will eventually reach 1 with any starting integer.<br>
<br>
So this page lets you try it out!<br><br>

</div>
<div style="float: right; width: 49%;">
<?php echo $collatz_out; ?>
</div>
</div>
<div id="rightside" style="width: 49%; float: left; padding-right: 10px;"><?php echo "\n" . $ct . "\n"; ?></div>
</body>
</html>