Project Euler - problem 006

August 1st, 2007 by Daniel

The sum of the squares of the first ten natural numbers is,
1² + 2² + … + 10² = 385

The square of the sum of the first ten natural numbers is,
(1 + 2 + … + 10)² = 55² = 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.

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
 
#include <iostream>
using namespace std;
 
#define interval 100
 
// define methods
int opphoyd(int num1, int num2);
 
int main ()
{
int res(1), sum1(0), sum2(0);
 
for(int i = 0; i <= interval; i ++){
sum1 += opphoyd(i , 2);
sum2 += i;
}
 
res = opphoyd(sum2, 2) - sum1;
 
//   cout << sum1 << " " << opphoyd(sum2, 2);
cout << "resultatet er ";
cout << res << "n";
 
return 0;
}
 
// methods
int opphoyd(int num1, int num2){
int res =1;
for(int i = 0 ; i < num2; i++){
res *= num1;
}
return res;
}

0 Responses to “Project Euler - problem 006”

  1. No Comments

Leave a Response