[Project Euler] Problem 6
2011-02-22 00:52
381 查看
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
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.
好吧,我们用最傻瓜的方式来做这道题
#include <iostream> using namespace std; int main(){ int sumOfTheSquare = 0; int squareOfTheSum = 0; int sum = 0; for (int i=1;i<=100;i++){ sumOfTheSquare +=i*i; sum += i; } squareOfTheSum = sum*sum; cout << squareOfTheSum-sumOfTheSquare << endl; return 0; }
好吧,你小时候一定不厌其烦的听你的老师给你讲高斯做加法的故事了
不管是真是假,我们有高斯公式和平方和公式
更简洁的公式法是
#include <iostream> using namespace std; int getSumOfTheSquare(int n){ return n*(n+1)*(2*n+1)/6; } int getSquareOfTheSum(int n){ int sum = n*(n+1)/2; return sum*sum; } int main(){ cout << getSquareOfTheSum(100)-getSumOfTheSquare(100) << endl; return 0; }
好吧,你也许会说,我可以直接把“和的平方”与“平方之和”的差用公式表示出来,可以一步求出结果
那就随便吧。
相关文章推荐
- 【Project Euler】【Problem 2】Even Fibonacci numbers
- [Project Euler]加入欧拉 Problem 12
- [Project Euler] Problem 2
- [Project Euler] Problem 19
- Project Euler:Problem 40 Champernowne's constant
- [Project Euler] Problem 29
- [Project Euler]Problem 1:Multiples of 3 and 5
- [Project Euler] Problem 18 & Problem 67 Python解答
- Project Euler:Problem 56 Powerful digit sum
- Project Euler:Problem 80 Square root digital expansion
- Project Euler__problem 6
- Project Euler - Problem 9
- [Project Euler] Problem 56
- [Project Euler]Problem 44
- 【Project Euler】【Problem 3】Largest prime factor
- [Project Euler]加入欧拉 Problem 17
- [Project Euler] Problem 20
- Project Euler: Problem 9 Special Pythagorean triplet
- [Project Euler]Problem 10
- [Project Euler] Problem 30