vlatkovski

Collatz conjecture

Aug 31st, 2016
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. vector<int> collatz(int x) {
  7.     vector<int> v;
  8.  
  9.     while (x != 1) {
  10.         if (x % 2 == 0) {
  11.             x /= 2;
  12.         } else {
  13.             x = 3 * x + 1;
  14.         }
  15.         v.push_back(x);
  16.     }
  17.  
  18.     return v;
  19. }
  20.  
  21. int main() {
  22.     for (;;) {
  23.         int n;
  24.         cin >> n;
  25.         cout << "> steps: " << collatz(n).size() << endl;
  26.     }
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment