Algorithm, Data structure/Solved Algorithmic Problem

Project Euler #6 - Sum square difference

JaykayChoi 2016. 6. 11. 13:00

The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)2 = 552 = 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.


출처: https://projecteuler.net/


1부터 100까지 자연수에 대해 합의 제곱과 제곱의 합의 차이를 구하는 문제입니다.

(a + b + c + d)^2 = a^2 + b^2 + c^2 + d^2 +2(ab + ac + ad + bc + bd + cd) 인 점을 이용하여 간단히 풀 수 있는 문제입니다.



my solving

c++

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
#include <iostream>
#include <string>
 
using namespace std;
 
 
int main() 
{
    int ret = 0;
    
    int maxNum = 100;
 
    for (int i = 1; i <= maxNum; i++)
    {
        for (int j = i + 1; j <= maxNum; j++)
        {
            ret += (i * j);
        }
    }
 
    ret *= 2;
 
 
    cout << ret << endl;
 
    system("pause");
    return 0;
}
cs