Created
March 25, 2026 08:04
-
-
Save juanfal/44ec9ad2b6118cfca24dac5977d81b88 to your computer and use it in GitHub Desktop.
collatz sequence all in main
This file contains hidden or 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
| // t04e20.collatz.cpp | |
| // juanfc 2026-03-25 | |
| // | |
| #include <iostream> | |
| using namespace std; | |
| int main() | |
| { | |
| int n; | |
| cin >> n; | |
| int steps = 0; | |
| cout << n; | |
| while (n != 1) { | |
| if (n % 2 == 0) { | |
| n /= 2; | |
| } else { | |
| n = 3 * n + 1; | |
| } | |
| cout << " " << n; | |
| steps++; | |
| } | |
| cout << " (" << steps << " steps)" << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment