Skip to content

Instantly share code, notes, and snippets.

@yesidays
Created June 26, 2012 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yesidays/2993382 to your computer and use it in GitHub Desktop.
Save yesidays/2993382 to your computer and use it in GitHub Desktop.
Algoritmo de Collatz
<form id="form1" name="form1" method="post" action="">
<table width="331" border="0">
<tr> <td width="124">Ingrese el número</td>
<td width="197"><input type="text" name="numero" id="numero" /></td>
</tr>
<tr> <td><input type="submit" name="baceptar" id="baceptar" value="Generar" /> </td></tr>
</table>
<? if ($_REQUEST['baceptar']!= ""){
if ($_REQUEST["numero"] != "") {
$resultado = $_REQUEST['numero'];
echo "<b>".$numero."</b><br />";
$pasos = 1;
while ($resultado != 1){
if ($resultado%2==0){
$total = $resultado / 2;
$resultado = $total;
echo $resultado."<br />";
}
else{
$total = ($resultado * 3) + 1;
$resultado = $total;
echo $resultado."<br />";
}
$pasos++;
}
}
}
echo "<br />Número total de pasos: <b>".$pasos."</b>";
?>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment