scosant
12/17/2012 - 11:21 AM

projecteuler006 - sum square difference

projecteuler006 - sum square difference

/* Scott Santarromana, 2010
 * projecteuler006 - sum square difference
 *
 * The sum of the squares of the first ten natural numbers is,
 * 1^2 + 2^2 + ... + 10^2 = 385
 * The square of the sum of the first ten natural numbers is,
 * (1 + 2 + ... + 10)^2 = 55^2 = 3025
 * Hence the difference between the sum of the squares of the
 * first ten natural numbers and the square of the sum
 * is 3025 - 385 = 2640.
 * Find the difference between the sum of the squares of
 * the first one hundred natural numbers and the square of
 * the sum.
 */

#include <iostream>

int64_t CalculateSquareOfSum(int64_t num);
int64_t CalculateSumOfSquares(int64_t num);

int main(int argc, char *argv[])
{
    int num = 100;
    int result = CalculateSumOfSquares(num) - CalculateSquareOfSum(num);
    std::cout << "Difference between sum of squares and square of sum"
              << std::endl << "of first " << num << " numbers is "
          << result << std::endl;
    return 0;
}

int64_t CalculateSquareOfSum(int64_t num) {
    int64_t result = 0;
    for (int64_t i = 1; i <= num; i++) {
        result += i;
    }
    return result * result;
}

int64_t CalculateSumOfSquares(int64_t num) {
    int64_t result = 0;
    for (int64_t i = 1; i <= num; i++) {
        result += i * i;
    }
    return result;
}