Project Euler #12: Highly divisible triangular number

  • + 0 comments

    include

    include

    include

    using namespace std;

    int main() { const int MAX_PRIMES = 1000; vector primes(MAX_PRIMES, 0); primes[0] = 2; primes[1] = 3; primes[2] = 5; primes[3] = 7; int count1 = 4; long long num = 9; while(count1 < MAX_PRIMES) { bool isprime = true; int sqrt_num = sqrt(num);

        for(int i = 0; primes[i] <= sqrt_num && i < count1; i++) {
            if(num % primes[i] == 0) {
                isprime = false;
                break;
            }
        }
        if(isprime) {
            primes[count1] = num;
            count1++;
        }
        num += 2;
    }
    
    int t;
    cin >> t;
    while(t--) {
        long long n;
        cin >> n;
    
        long long sum = 1;
        long long result = 1;
        long long j = 2;
    
        while(true) {
            sum += j;
            long long temp = sum;
            result = 1;  
            for(int k = 0; k < count1 && primes[k] > 0; k++) {
                int count2 = 1;
                while(temp % primes[k] == 0) {
                    temp /= primes[k];
                    count2++;
                }
                result *= count2;
                if(temp == 1) {
                    break;
                }
            }
            if(result > n) {
                break;
            }
            j++;
        }
        cout << sum << endl;
    }
    return 0;
    

    }