Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created March 25, 2026 08:04
Show Gist options
  • Select an option

  • Save juanfal/44ec9ad2b6118cfca24dac5977d81b88 to your computer and use it in GitHub Desktop.

Select an option

Save juanfal/44ec9ad2b6118cfca24dac5977d81b88 to your computer and use it in GitHub Desktop.
collatz sequence all in main
// 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