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

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

Problem 6 Sum square difference

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

1 2 + 2 2 + 3 2 + ⋯ + 1 0 2 = 385 \large 1^2+2^2+3^2+\cdots+10^2=38512+22+32++102=385

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

( 1 + 2 + 3 + ⋯ + 10 ) 2 = 5 5 2 = 3025 \large (1+2+3+\cdots+10)^2=55^2=3025(1+2+3++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 \large 3025 - 385 = 26403025385=2640

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

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

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

1 2 + 2 2 + 3 2 + ⋯ + 1 0 2 = 385 \large 1^2+2^2+3^2+\cdots+10^2=38512+22+32++102=385

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

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

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

3025 − 385 = 2640 \large 3025 - 385 = 26403025385=2640

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

思路分析

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

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

自然数和的平方通项公式

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

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

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

= n ( n − 1 ) ( n + 1 ) ( 3 n + 2 ) 12 \large =\frac{n(n-1)(n+1)(3n+2)}{12}=12n(n1)(n+1)(3n+2)

代码实现

/*
 * @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



目录
打赏
0
0
0
0
35
分享
相关文章
|
9月前
|
API
PTA-给定精度,求圆周率PI的近似值
给定精度,求圆周率PI的近似值
145 1
平方根倒数快速算法
平方根倒数快速算法
96 0
|
8月前
|
NumPy 舍入小数、对数、求和和乘积运算详解
NumPy 提供五种舍入小数的方法:`trunc()`, `fix()`, `around()`, `floor()`, `ceil()`。此外,它还支持对数运算,如 `log2()`, `log10()`, `log()`,以及自定义底数的对数。NumPy 的 `sum()` 和 `prod()` 函数用于数组求和与乘积,可指定轴进行计算,`cumsum()` 和 `cumprod()` 实现累积求和与乘积。关注公众号 &quot;Let us Coding&quot; 获取更多内容。
84 2
|
9月前
绝对值最大
绝对值最大
58 1
(二维vector)(绝对值求和等式的处理)B. Playing in a Casino
(二维vector)(绝对值求和等式的处理)B. Playing in a Casino
104 0
【欧拉计划第 8 题】序列中最大的乘积 Largest product in a series
【欧拉计划第 8 题】序列中最大的乘积 Largest product in a series
152 0
【欧拉计划第 8 题】序列中最大的乘积 Largest product in a series
【CCCC】L2-018 多项式A除以B (25分),多项式除法
【CCCC】L2-018 多项式A除以B (25分),多项式除法
192 0
数学:简单理解指数、对数、乘方、开方
数学:简单理解指数、对数、乘方、开方
731 0
数学:简单理解指数、对数、乘方、开方
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等