Created
June 10, 2011 20:20
-
-
Save thomas-p-wilson/1019681 to your computer and use it in GitHub Desktop.
Collatz Conjecture Solver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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