Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 16, 2022 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/d10bc6619c2b99461cb61df3674c294f to your computer and use it in GitHub Desktop.
Save parzibyte/d10bc6619c2b99461cb61df3674c294f to your computer and use it in GitHub Desktop.
#include <stdio.h>
unsigned long long collatz(unsigned long long numero)
{
// https://parzibyte.me/blog
unsigned long long iteraciones = 0;
while (numero != 1)
{
if (numero % 2 == 0)
{
numero = numero / 2;
}
else
{
numero = (3 * numero) + 1;
}
// Si no quieres imprimir, simplemente elimina o comenta la siguiente línea
printf("%llu,", numero);
iteraciones++;
}
return iteraciones;
}
int main()
{
unsigned long long numero = 27;
unsigned long long iteraciones = collatz(numero);
printf("\nLas iteraciones para llegar a 1 desde %llu fueron: %llu\n", numero, iteraciones);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment