Skip to content

Instantly share code, notes, and snippets.

@CodeMaster7000
Last active April 29, 2022 20:36
Show Gist options
  • Save CodeMaster7000/0aa49cb8e7d8d2406abfae93b035bdfe to your computer and use it in GitHub Desktop.
Save CodeMaster7000/0aa49cb8e7d8d2406abfae93b035bdfe to your computer and use it in GitHub Desktop.
A simple program coded in PHP which prints the Collatz sequence for the inputted number.
<?php
function printCollatz($n)
{
while ($n != 1)
{
echo $n . " ";
if ($n & 1)
$n = 3 * $n + 1;
else
$n = $n / 2;
}
echo $n;
}
//Enter the number you wish to input here.
printCollatz(40);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment