Project Euler #44: Pentagon numbers

  • + 0 comments
    #include <bits/stdc++.h>
    using namespace std;
    #define ull unsigned long
    
    set<ull> pent;
    
    void generate() {
        ull temp;
        for(ull n = 1; n <= 1000000; n++) {
            temp = n * (3 * n - 1) / 2;
            pent.insert(temp);
        }
    }
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        generate();
        ull N, K;
        cin >> N >> K;
        for(ull n = K + 1; n <= N; n++) {
            ull diff = n * (3 * n - 1) / 2 - (n - K) * (3 * (n - K) - 1) / 2;
            ull add = n * (3 * n - 1) / 2 + (n - K) * (3 * (n - K) - 1) / 2;
            if(pent.count(diff) || pent.count(add)) {
                cout << n * (3 * n - 1) / 2 << endl;
            }
        }
        return 0;
    }
    

    Accurate and Perfect solution of this problme