Skip to content

Instantly share code, notes, and snippets.

@odarbelaeze
Last active December 16, 2015 05:59
Show Gist options
  • Save odarbelaeze/5388496 to your computer and use it in GitHub Desktop.
Save odarbelaeze/5388496 to your computer and use it in GitHub Desktop.
The 3n + 1 problem with some error
#include <iostream>
int cycle_lenght (int i)
{
int lenght = 1;
int n = i;
while (n != 1)
{
n = n % 2 == 0? n / 2 : 3 * n + 1;
lenght++;
}
return lenght;
}
int main(int argc, char const *argv[])
{
while(!std::cin.eof())
{
int i, j, min, max, lenght, maxlength = 1;
std::cin >> i >> j;
min = i < j? i : j;
max = i < j? j : i;
while (min <= max)
{
lenght = cycle_lenght(min++);
if (lenght > maxlength)
maxlength = lenght;
}
std::cout << i << " " << j << " " << maxlength << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment