Pythagorean quadruples | Rosetta Code | #9

URL to the problem page: https://rosettacode.org/wiki/Pythagorean_quadruples


One form of Pythagorean quadruples is (for positive integers a, b, c, and d):
a2 + b2 + c2 = d2

     An example:
22 + 32 + 62 = 72
which is: 4 + 9 + 36 = 49

For positive integers up 2,200 (inclusive), for all values of a, b, c, and d, find (and show here) those values of d that can't be represented.

(Note: this task takes too much time if you compile the program in Debug Mode/x86. I recommend to compile the program in Release Mode/x64, and with the OpenMP if possible. (by including <omp.h> library, enabling OpenMP from the project settings and using " #pragma omp parallel for " in the code.) Because it reduces the amount of time to complete the task even more.)



#include <iostream>
#include <omp.h>
using namespace std;

int main()
{
#pragma omp parallel for
    for (long long int i = 1; i <= 2200; i++) {
        int cnt = 0;
        for (long long int j = 1; j < 2200; j++) {
            if (pow(j, 2) > pow(i, 2)) {
                break;
            }
            for (long long int k = 1; k < 2200; k++) {
                if (pow(k, 2) > pow(i, 2) || (pow(j, 2) + pow(k, 2)) > pow(i, 2)) {
                    break;
                }
                for (long long int l = 1; l < 2200; l++) {
                    if ((pow(j, 2) + pow(k, 2) + pow(l, 2)) == pow(i, 2)) {
                        cnt++;
                        break;
                    }
                    else if (pow(l, 2) > pow(i, 2) || pow(k, 2) + pow(l, 2) > pow(i, 2) || (pow(j, 2) + pow(k, 2) + pow(l, 2)) > pow(i, 2)) {
                        break;
                    }
                }
                if (cnt != 0) {
                    break;
                }
            }
            if (cnt != 0) {
                break;
            }
        }
        if (cnt == 0) {
            cout << i << " can not be represented." << endl;
        }
    }
    return 0;
}



Comments

Post a Comment

My photo
Ercan Tomac
instagram.com/ercantomac