Skip to content

Instantly share code, notes, and snippets.

@thomas-p-wilson
Created June 10, 2011 20:20
Show Gist options
  • Save thomas-p-wilson/1019681 to your computer and use it in GitHub Desktop.
Save thomas-p-wilson/1019681 to your computer and use it in GitHub Desktop.
Collatz Conjecture Solver
<?php
if(!isset($_GET['number']) || empty($_GET['number'])) {
echo '<h3>Please enter a number > 1</h3>';
} elseif(!is_numeric($_GET['number'])) {
echo '<h3>It has to be a number.</h3>';
}
?>
<form action="" method="get">
<input type="text" name="number">
<input type="submit" value="Go">
</form>
<?php
if(isset($_GET['number']) && !empty($_GET['number']) && is_numeric($_GET['number'])) {
echo '<hr>';
$number = $_GET['number'];
echo $number.'<br>';
do {
if($number % 2 == 0) {
$number = $number / 2;
} else {
$number = $number * 3 + 1;
}
echo $number.'<br>';
} while($number > 1);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment