Solutions
CSES
CSES
  • Home
  • CSES Problem Set
    • Introductory Problems
      • Weird Algorithm
Powered by GitBook
On this page

Was this helpful?

  1. CSES Problem Set
  2. Introductory Problems

Weird Algorithm

PreviousIntroductory Problems

Last updated 2 years ago

Was this helpful?

#include <iostream>
using namespace std;
 
void solution(long n){
    while(n != 1){
        cout << n << " ";
        if(n % 2 == 0){
            n = n / 2;
        }
        else{
            n = n * 3 + 1;
        }
    } 
    cout << 1;
}
 
int main() {
    long input;
    cin >> input;
    solution(input);
    return 0;
}
CSES - Weird Algorithm