Time limit

My issue

time limit execded

My code

def count_factors(n, factor_dict):
    if n not in factor_dict:
        count = 0
        for i in range(1, int(n**0.5) + 1):
            if n % i == 0:
                count += 1
                if i != n // i:
                    count += 1
        factor_dict[n] = count
    return factor_dict[n]

def main():
    t = int(input())
    factor_dict = {}

    for _ in range(t):
        x = int(input())

        max_factor_count = 1
        for i in range(2, int(x**0.5) + 1):
            if x % i == 0:
                factor_count_1 = count_factors(i, factor_dict)
                factor_count_2 = count_factors(x // i, factor_dict)

                max_factor_count = max(max_factor_count, factor_count_1, factor_count_2)

        if max_factor_count == 1:
            print(x)
        else:
            print(max_factor_count)

if __name__ == "__main__":
    main()

Learning course: 2000 to 2500 difficulty problems
Problem Link: Maximum Factors Problem Practice Problem in 2000 to 2500 difficulty problems - CodeChef

@charan2015
here bro plzz refer my c++ code for better understanding of the logic and implementation

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        map<int,int> mp;
        while(n%2==0)
        {
            n=n/2;
            mp[2]++;
        }
        for(int i=3;i<=sqrt(n);i+=2)
        {
            if(n%i==0)
            {
                while(n%i==0)
                {
                    n=n/i;
                    mp[i]++;
                }
            }
        }
        if(n>2)
        mp[n]++;
        int mx=0;
        for(auto x:mp)
        {
            mx=max(mx,x.second);
        }
        for(auto x:mp)
        {
            if(x.second==mx)
            {
                cout<<x.first<<endl;
                break;
            }
        }
    }
}