-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler006.cpp
More file actions
51 lines (35 loc) · 993 Bytes
/
Copy patheuler006.cpp
File metadata and controls
51 lines (35 loc) · 993 Bytes
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
49
50
51
/*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> //for pow()
using namespace std;
int sumOfSquares(int max)
{
int result = 1;
for(int a=2; a<=max; a++)
{
result += pow(a,2);
}
return result;
}
int squareOfSums(int max)
{
int result = 1;
for(int a=2; a<=max; a++)
{
result += a;
}
return pow(result,2);
}
int main(){
//BRUTE FORCE!!!!
//probably a better way to do this, but for N=100, result is still computed without a noticable delay
cout << squareOfSums(100) - sumOfSquares(100) << endl;
return 0;
}