Skip to content

Instantly share code, notes, and snippets.

@kylethedeveloper
Created April 11, 2018 08:57
Show Gist options
  • Save kylethedeveloper/2d29b1b5bb1ed7ed161995d9167e5a83 to your computer and use it in GitHub Desktop.
Save kylethedeveloper/2d29b1b5bb1ed7ed161995d9167e5a83 to your computer and use it in GitHub Desktop.
Project Euler - Problem 6 - Sum square difference
/*
Project_6.cpp : 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>
using namespace std;
int main()
{
int sum1 = 0, sum2 = 0;
for (int i = 1; i < 101; i++)
{
sum1 += i * i;
}
for (int j = 0; j < 101; j++)
{
sum2 += j;
}
cout << "Result is:\n" << sum2 * sum2 << " - " << sum1 << " = " <<
(sum2*sum2) - sum1 << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment