Project Euler #14: Longest Collatz sequence

  • + 0 comments

    Can anyone help reduce runtime of this code

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int ColSeq(int x){
        int count = 0;
        while(x>1){
            if(x%2==0)
                x /= 2;
            else
                x = 3*x + 1;
            count++;
        }
        return count;
    }
    
    int main() {
        int t;
        scanf("%d",&t);
        while(t--){
            int n;
            scanf("%d",&n);
            int res;
            int max_res = 0;
            int max_x;
            for(int i=n;i>2;i++){
                res = ColSeq(i);
                if(res>max_res){
                    max_res = res;
                    max_x = i;
                }
            }
            printf("%d\n",max_x);
        }  
        return 0;
    }