-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0006_sum_square_difference.cpp
More file actions
48 lines (36 loc) · 1.42 KB
/
0006_sum_square_difference.cpp
File metadata and controls
48 lines (36 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**********************
Author: Daniel Bowder
Date: July 21, 2018
Problem: https://projecteuler.net/problem=6
"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>
#include <cmath>
using std::cout;
using std::endl;
int sum_square_difference(double value) {
// Setup placeholder variables for the sum of squares and for the square of the sum.
double sumSquares = 0;
double squareSum = 0;
// For each number from 1 to our value
for (int i=1; i<=value;i++) {
// Calculate the square value of the number and add it to our sum of squres
sumSquares+=pow(i,2);
// Add the value of our number to the square sum. Later we will square this sum.
squareSum+=i;
}
// Square the sum of all values from 1 to our value
squareSum = pow(squareSum,2);
// Return the difference between the square of our sum, and the sum of our squares.
return squareSum - sumSquares;
}
int main(int argc, char* argv[]) {
cout << "Problem 6. " << sum_square_difference(100) << endl;
}