题目6:平方和与和平方的差是多少?
本帖最后由 永恒的蓝色梦想 于 2020-8-30 12:33 编辑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。
找出前一百个自然数的平方和与和平方的差。
本帖最后由 永恒的蓝色梦想 于 2020-8-30 22:29 编辑
#include <stdio.h>
#include <math.h>
int main(void)
{
int i,pr,hr0,hr,result;
pr = hr = hr0 = 0;
for(i=1;i<=100;i++)
{
pr =i * i + pr;
}
for(i=1;i<=100;i++)
{
hr0 = i + hr0;
}
hr = hr0 * hr0;
result = abs(pr - hr);
printf("%d\n",result);
}
如果有错误希望指出 本帖最后由 无名侠 于 2015-7-10 18:41 编辑
感觉Python就是牛逼
sum(range(1,101))**2-sum()
25164150 #include <stdio.h>
#include<windows.h>
int main()
{
int i;
long long int j = 0,k;
for(i = 1;i<=100;++i)
{
j += i*i;
}
k = 5050*5050;
printf("%lld\n",k - j);
system("pause");
return 0;
} 无名侠 发表于 2015-7-10 18:37
感觉Python就是牛逼
sum(range(1,101))**2-sum()
25164150
花了多久显示出来的?? 牡丹花下死做鬼 发表于 2015-7-19 22:09
花了多久显示出来的??
小于1秒 /*找出前一百个自然数的平方和与和平方的差*/
#include <iostream>
using namespace std;
int main ()
{
int sum1 = 0; //前一百个自然数的平方和
int sum2 = 0; //前一百个自然数的和的平方
int i; //自然数
for ( i = 1 ; i <= 100 ; i ++ )
{
sum1 += i * i ;
}
for ( i = 1 ; i <= 100 ; i ++ )
{
sum2 += i ;
}
sum2 *= sum2;
i = sum2 - sum1 ;
cout<<"前一百个自然数的平方和与和平方的差为:"<<i<<endl;
return 0 ;
}
输出:前一百个自然数的平方和与和平方的差为:25164150 sum1,sum2=0,0
for i in range(1,101):
sum1+=i
sum2+=i**2
print(sum1**2-sum2)
答案25164150 def x(n):
a,b=0,0
for i in range(1,n+1):
a+=(i*i)
for i in range(1,n+1):
b+=i
return (b*b)-a result1 = []
result2 = []
for x in range(1, 101):
result1.append(x)
for y in range(1, 101):
result2.append(pow(y, 2))
print(pow(sum(result1), 2) - sum(result2))
看看自己写的再看看大神写的,感觉惨不忍睹{:5_96:} print(sum(range(1, 101))**2 - sum(i**2 for i in range(1, 101)))
25164150 #include "stdio.h"
#define Num 100
void main()
{
int i,j;
long sum=0;
for(i=2;i<=Num;i++)
for(j=1;j<i;j++)
sum=i*j+sum;
printf("%d\n",sum*2);
} #include <stdio.h>
int main()
{
unsigned int add=0,result=0,i=2;
for(;i!=101;i++){
add+=(i-1);
result+=i*add;
}
printf("%u\n\n",result*2);
return 0;
} def euler(x):
return sum()**2 - sum()
if __name__ == '__main__':
print(euler(100)) sum1 = 0
sum2 = 0
for i in range(101):
sum1 = sum1 + i**2
sum2 = sum2 + i
a = sum2 ** 2 - sum1
print(a)
25164150 list1 = []
count = 0
for i in range(1, 101):
list1.append(i*i)
count += i
# print sum(list1)
# print count*count
print count*count - sum(list1)
25164150
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM 100
int main()
{
int i;
long s1,s2;
for(i = 0; i<NUM; i++)
{
s1+=i*i;
s2+=i;
}
s2*=s2;
printf("%ld",abs(s2-s1));
return 0;
}
#找出前一百个自然数的平方和与和平方的差
import time
start=time.clock()
def pfh(n):
s=0
for i in range(1,n+1):
s=s+i**2
return s
def hpf(n):
s=0
for i in range(1,n+1):
s=s+i
return s**2
print(hpf(100)-pfh(100))
end=time.clock()
print("耗时:"+str(end-start)+"秒。") public class SumSquareDifference {
public static void main(String[] args){
int n = 100;
int sum = (1 + n) * n / 2;
int sum_Square = sum * sum;
int square_Sum = n * (n + 1) * (2 * n + 1) / 6;
System.out.println("前100个数平方和与和的平方之间的差值是:" + (square_Sum - sum_Square));
}
} import time
t = time.clock()
def euler06(count=10):
return (sum(n for n in range(1,count+1)))**2 - sum()
print(euler06(100),' time:',time.clock()-t)
25164150time: 0.00012197957453998107