Weird Algorithm

Bruteforce
View Original
Back to Introductory Problems

Weird Algorithm

Consider an algorithm that takes as input a positive integer n. If n is even, the algorithm divides it by two, and if n is odd, the algorithm multiplies it by three and adds one. The algorithm repeats this, until n is one.

For example, the sequence for n=5 is as follows: 5 → 16 → 8 → 4 → 2 → 1

Your task is to simulate the execution of the algorithm for a given value of n.

Input

The only input line contains an integer n.

Output

Print a line that contains all values of n during the algorithm.

Constraints

  • 1 ≤ n ≤ 10^6

Example

Input:

5

Output:

5 16 8 4 2 1

Hints

Hint 1: Understanding the Pattern

Look at what happens to the number in each step:

  • If it's even (divisible by 2), divide by 2
  • If it's odd, multiply by 3 and add 1

Hint 2: Implementation Strategy

You don't need to store all the numbers. Just process them one by one and print as you go.

Hint 3: Loop Condition

Keep repeating the process until the number becomes 1. The sequence always terminates at 1.

Hint 4: Example Walkthrough

For n = 5:

  • 5 is odd → 5 × 3 + 1 = 16
  • 16 is even → 16 ÷ 2 = 8
  • 8 is even → 8 ÷ 2 = 4
  • 4 is even → 4 ÷ 2 = 2
  • 2 is even → 2 ÷ 2 = 1
  • Stop at 1

Hint 5: Edge Cases

  • What happens when n = 1? (Just print 1)
  • Make sure to print the initial value of n as well

Solution

This problem asks us to implement the Collatz conjecture sequence, also known as the 3n+1 problem.

Algorithm

The algorithm is straightforward:

  1. Start with the given number n
  2. Print n
  3. While n is not equal to 1:
    • If n is even: n = n / 2
    • If n is odd: n = 3n + 1
    • Print n

Implementation Details

  • We need to handle the sequence step by step
  • Print each number in the sequence separated by spaces
  • The sequence always ends at 1 (this is the Collatz conjecture, though unproven, it holds for all tested values)

Complexity Analysis

  • Time Complexity: O(log n) on average, though the exact complexity is unknown due to the unproven nature of the Collatz conjecture
  • Space Complexity: O(1) - we only need to store the current value

Key Insights

  1. Simple Simulation: This is a direct simulation problem - just follow the rules
  2. No Optimization Needed: The constraints are small enough that a direct approach works
  3. Integer Operations: Be careful with integer division (though not an issue in this problem)
  4. Termination: The sequence always reaches 1 for the given constraints

Implementation

C++

                                #include<bits/stdc++.h>
using namespace std;
#define INF 2147483647
#define INFL 9223372036854775807
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define take(x) scanf("%d",&x)
#define DE(x) printf("\ndebug %d\n",x);
#define vout(x) for(int i=0;i<x.size();i++) cout<<x[i]<<" ";
#define pie acos(-1)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int main(){
    int t = 1;
    //cin>>t;
    while(t--){
        ll n;
        cin>>n;
        while(n>1){
            printf("%lld ",n);
            if( n%2 == 0 ) n/=2;
            else n = n*3+1;
        }
        cout<<1;
    }
}