鱼C论坛

 找回密码
 立即注册
查看: 597|回复: 13

题目6:找出前100个自然数的“和的平方”与“平方的和”之差

[复制链接]
发表于 2023-5-15 05:23:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目6:找出前100个自然数的“和的平方”与“平方的和”之差


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。

那么请找出前 100 个自然数的 “和的平方” 与 “平方的和” 之差?


视频讲解:




思路解析及源码参考(C & Python):

游客,如果您要查看本帖隐藏内容请回复



评分

参与人数 1荣誉 +2 收起 理由
zhangjinxuan + 2 无条件支持楼主!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-15 19:45:45 | 显示全部楼层
  1. #include <bits/stdc++.h>
  2. using namespace std;

  3. long long res = 0;
  4. int n;

  5. int main() {
  6.         //scanf("%d", &n);
  7.         n = 100;
  8.         for (int i = 1; i <= n; ++i) {
  9.                 res += i * (i + 1 + n) * (n - i);
  10.         }
  11.         printf("%lld\n", res);
  12.         return 0;
  13. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-16 09:34:21 | 显示全部楼层
  1. #include <bits/stdc++.h>
  2. using namespace std;

  3. int a,b;

  4. int main()
  5. {
  6.     for(int i=1;i<=100;i++) a += i*i,b += i;
  7.     cout<<b*b-a<<endl;
  8.     return 0;
  9. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2023-5-17 16:58:40 | 显示全部楼层
  1. print(sum(range(1, 101)) ** 2 - sum(x ** 2 for x in range(1, 101)))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-29 00:39:09 | 显示全部楼层
这个简单
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-2 14:08:24 | 显示全部楼层
good object
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-6-7 14:05:11 | 显示全部楼层
# 答案25164150
sum_of_squares = 0
squares_of_sum = 0

for i in range(1, 101):
    sum_of_squares += pow(i, 2)
    squares_of_sum += i

print(f'前100个自然数的“和的平方”与“平方的和”之差:{pow(squares_of_sum, 2) - sum_of_squares}')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-3 07:01:05 | 显示全部楼层
hdpf = sum(range(1, 101)) ** 2
pfdh = sum([i ** 2 for i in range(1, 101)])
print(hdpf - pfdh)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-7 17:01:14 | 显示全部楼层
0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-1 15:15:44 | 显示全部楼层
1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-28 21:24:50 | 显示全部楼层
直接模拟
  1. Ulong test6 (Ulong n)
  2. {
  3.     Ulong sumpo2=0;
  4.     Ulong sumpo3=0;
  5.     for(int i=1;i<=n;i++)
  6.     {
  7.       sumpo2+=i*i;
  8.       sumpo3+=i;
  9.     }
  10.     sumpo3*=sumpo3;
  11.     return sumpo3-sumpo2;
  12. }
  13. int main (int argc, char *argv[])
  14. {
  15.   auto res = test6 (100);
  16.   std::cout << res << std::endl;
  17. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-28 21:31:35 | 显示全部楼层
复杂度O1的数学解
  1. Ulong test6 (Ulong n)
  2. {
  3.     auto sumpow2=[](int n){return (n * (n + 1) * (2*n + 1)) / 6;};
  4.     auto sumpow3=[](int n){return (n*n*(n+1)*(n+1))/4;};
  5.     return sumpow3(n)-sumpow2(n);
  6. }
  7. int main (int argc, char *argv[])
  8. {
  9.   auto res = test6 (100);
  10.   std::cout << res << std::endl;
  11. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-1-4 12:21:44 From FishC Mobile | 显示全部楼层
test
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-1-4 15:03:23 | 显示全部楼层
看不明白,先学习下吧,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-21 10:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表