您的位置:首页 > 其它

Project Euler__problem 6

2017-10-11 18:35 417 查看

Problem 6

Sum square difference

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.

平方的和与和的平方之差

前十个自然数的平方的和是

12 + 22 + … + 102 = 385
前十个自然数的和的平方是

(1 + 2 + … + 10)2 = 552 = 3025
因此前十个自然数的平方的和与和的平方之差是 3025 − 385 = 2640。

求前一百个自然数的平方的和与和的平方之差。

这次做到第6题,感觉没有什么好总结的,

比较简单的一道题目,也算是这样吧

答案是25164150

#include<iostream>

int fun1(int n)
{
int sum = 0;
int i;
for (i = 1; i <= n; i++)
{
sum = sum + i*i;
}
return sum;
}

int fun2(int n)
{
int sum = 0;
int i;
for (i = 1; i <= n; i++)
sum = sum + i;
return sum*sum;
}

int main()
{
int x1, x2;
x1 = fun1(100);
x2 = fun2(100);
std::cout << x2 << "-" << x1 << "=" << x2 - x1 << std::endl;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: