Last active
December 16, 2015 05:59
-
-
Save odarbelaeze/5388496 to your computer and use it in GitHub Desktop.
The 3n + 1 problem with some error
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
#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