6.21.2010

Problem 6 - Project Euler Problems - Squares Sum Squared

After some disappointment with Problem 5, I got to wet my appetite with some basic computation. Problem 6 requires you to find the difference between the sum of the numbers between a range and the sun of the suares of the same numbers between the same range.

The code I used is as below:
#include <iostream>
using namespace std;

int main (int argc, char * const argv[]) {
  cout << "PROJECT EULER PROBLEM 6\n" << endl;
 
  int start(1), end(100), sum(0), squareSum(0), sumSquare(0);

  for (int i = start; i <= end; ++i) {
    int square = i * i;
    squareSum += square;
    sum += i;
  }
 
  sumSquare = sum * sum;
 
  cout << sumSquare - squareSum << endl;
  return 0;
}
The result is 25164150.

No comments: