All pastes #194887 Raw Edit

SumFourSquares

public cpp v1 · immutable
·published 2006-10-08 16:34 UTC
rendered paste body
void SumFourSquares(int n) {    int i[4] = {0, 0, 0, -1}, x;    double timer = - clock();    try {        for(;;) {        ++i[3];        x = i[0]*i[0] + i[1]*i[1] + i[2]*i[2] + i[3]*i[3];        for (int c = 3; x > n; --c) {            if (c == 0) throw(0);            ++i[c-1];            for(int d = c; d <= 3; ++d) i[d] = i[c-1];            x = i[0]*i[0] + i[1]*i[1] + i[2]*i[2] + i[3]*i[3];        }        if (x == n)            cout << setw(5) << i[0]      << " " << setw(5) << i[1]      << " "                 << setw(5) << i[2]      << " " << setw(5) << i[3]      << " --> "                 << setw(5) << i[0]*i[0] << " " << setw(5) << i[1]*i[1] << " "                 << setw(5) << i[2]*i[2] << " " << setw(5) << i[3]*i[3] << " --> "                 << setw(5) << x << endl;                  }    }    catch(int e) {        cout << "Time elapsed: " << (timer + clock())/CLK_TCK << "s" << endl;    }    return;}