Skip to content

Instantly share code, notes, and snippets.

@OldBigBuddha
Created October 20, 2020 04:15
Show Gist options
  • Select an option

  • Save OldBigBuddha/112ee69bbdf3d3995064e1dd8cdf8656 to your computer and use it in GitHub Desktop.

Select an option

Save OldBigBuddha/112ee69bbdf3d3995064e1dd8cdf8656 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int collatz(int n) {
if (n == 1) {
cout << endl;
return n;
}
int buf = n;
if (n%2 == 0) {
buf = buf / 2;
} else {
buf = n*3 + 1;
}
cout << " => " << buf;
return collatz(buf);
}
int main() {
int n;
cout << "Number: ";
cin >> n;
cout << n;
collatz(n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment