博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(Problem 6)Sum square difference
阅读量:4307 次
发布时间:2019-06-06

本文共 1079 字,大约阅读时间需要 3 分钟。

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

1
2 + 2
2 + ... + 10
2 = 385

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

(1 + 2 + ... + 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 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.

1 #include 
2 #include
3 #include
4 #include
5 6 #define N 100 7 8 int powplus(int n, int k) 9 {10 int s=1;11 while(k--)12 {13 s*=n;14 }15 return s;16 }17 18 int sum1(int n)19 {20 return powplus((n+1)*n/2,2);21 } 22 23 int sum2(int n)24 {25 return (n*(n+1)*(2*n+1))/6;26 }27 28 void solve()29 {30 printf("%d\n",sum1(N));31 printf("%d\n",sum2(N));32 printf("%d\n",sum1(N)-sum2(N));33 }34 35 int main()36 {37 solve();38 return 0;39 }

 

Answer:
25164150

 

转载于:https://www.cnblogs.com/cpoint/p/3367349.html

你可能感兴趣的文章
Neutron在给虚拟机分配网络时,底层是如何实现的?
查看>>
netfilter/iptables全攻略
查看>>
Overlay之VXLAN架构
查看>>
Eclipse : An error occurred while filtering resources(Maven错误提示)
查看>>
在eclipse上用tomcat部署项目404解决方案
查看>>
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>
MapReduce的 Speculative Execution机制
查看>>
大数据学习之路------借助HDP SANDBOX开始学习
查看>>
Hadoop基础学习:基于Hortonworks HDP
查看>>
为什么linux安装程序 都要放到/usr/local目录下
查看>>
Hive安装前扫盲之Derby和Metastore
查看>>
永久修改PATH环境变量的几种办法
查看>>
大数据学习之HDP SANDBOX开始学习
查看>>
Hive Beeline使用
查看>>