前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【欧拉计划第 6 题】和的平方与平方的和差值 Sum square difference

【欧拉计划第 6 题】和的平方与平方的和差值 Sum square difference

作者头像
攻城狮杰森
发布2022-06-03 13:23:20
3720
发布2022-06-03 13:23:20
举报
文章被收录于专栏:技术集锦技术集锦

Problem 6 Sum square difference

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

\large 1^2+2^2+3^2+\cdots+10^2=385

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

\large (1+2+3+\cdots+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:

\large 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.

问题 6 和的平方与平方的和差值

前十个自然数的平方的和为:

\large 1^2+2^2+3^2+\cdots+10^2=385

而前十个自然数和的平方为:

\large (1+2+3+\cdots+10)^2=55^2=3025

因此,前十个自然数的平方和与和的平方之间的差是:

\large 3025 - 385 = 2640

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

思路分析

自然数的平方的和通项公式

\large S(1)=\frac{n(n+1)(2n+1)}{6}

自然数和的平方通项公式

\large S(2)=\left ( \frac{n(n+1)}{2} \right )^2

则和的平方与平方和差值通项公式为

\large S(n)=S(2)-S(1)=\left ( \frac{n(n+1)}{2} \right )^2-\frac{n(n+1)(2n+1)}{6}
\large =\frac{n(n-1)(n+1)(3n+2)}{12}

代码实现

代码语言:javascript
复制
/*
 * @Author: coder-jason
 * @Date: 2022-04-12 10:48:07
 * @LastEditTime: 2022-04-12 11:16:45
 */
#include <iostream>
using namespace std;

int main()
{
    int n = 100;
    long long int ans = n * (n - 1) * (n + 1) * (3 * n + 2) / 12;
    cout << ans;
    return 0;
}

答案:25164150

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-04-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem 6 Sum square difference
  • 问题 6 和的平方与平方的和差值
  • 思路分析
  • 代码实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档