leon0149
发表于 2020-5-9 10:10:46
#include <stdio.h>
int main(void)
{
int sum = 0;
for (int i = 1; i <= 100; ++i) {
for (int j = 1; j <= 100; ++j) {
if (j != i) {
sum += i * j;
}
}
}
printf("%d", sum);
return 0;
}
数据小随从
发表于 2020-6-11 13:11:48
本帖最后由 永恒的蓝色梦想 于 2020-8-30 22:29 编辑
#include<stdio.h>
#include<stdlib.h>
long sum(long num)//求平方和
{
long res1 = 0;
for (int i = 1; i <= num; i++)
{
res1 += i * i;
}
return res1;
}
long square(long num)//求和的平方
{
long res2 = 0;
for (int i = 1; i <= num; i++)
res2 += i;
return res2 * res2;
}
void main()
{
long a, b, num;
printf("请输入一个整数\n");
scanf_s("%ld", &num);
a = sum(num);
b = square(num);
if (a > b)
{
printf("方和与和的平方的差 = %ld\n", a - b);
}
else
{
printf("方和与和的平方的差 = %ld\n", b - a);
}
system("pause");
}
yhhpf
发表于 2020-8-24 13:32:08
sum1 = 0
for i in range(1,101):
sum1 += i ** 2
sum1 = sum(range(1,101))**2 - sum1
print(sum1)
4444567
发表于 2020-8-30 20:57:36
from time import time
x = int(input('请输入所求的数字:'))
t1 = time()
def pfh(x):
a = 0
for i in range(x+1):
a = a + i*i
return a
def hpf(x):
b = 0
for i in range(x+1):
b = b + i
return b*b
print('所求平方和与和平方的差值为:%d' % (hpf(x)-pfh(x)))
t2 = time()
t=t2-t1
print('耗时为:%s' % t)
有马_冬巳
发表于 2020-10-2 16:19:51
'''十个自然数的平方和是:12 + 22 + ... + 102 = 385
前十个自然数的和的平方是:(1 + 2 + ... + 10)2 = 552 = 3025
所以平方和与和的平方的差是 3025 - 385 = 2640。
找出前一百个自然数的平方和与和平方的差。'''
def diff(num):
sum_square = 0
square_sum = 0
sum = 0
for i in range(1, num+1):
sum_square += i**2
for j in range(1, num+1):
sum += j
square_sum = sum**2
difference = square_sum - sum_square
print("前%d个自然数的平方和与和平方的差是: %d" %(num,difference))
start_diff = time.time()
diff(100)
time_diff = time.time() - start_diff
print("%f秒" %time_diff)
前100个自然数的平方和与和平方的差是: 25164150
0.000499秒
gonorth
发表于 2020-11-5 11:33:12
def add_square(n):
count = 0
for i in range(n + 1):
count += (i ** 2)
return count
def square_add(n):
count = 0
for i in range(n + 1):
count += i
count = count ** 2
return count
def sub_s(n):
return (square_add(n) - add_square(n))
print(add_square(100))
print (square_add(100))
print(sub_s(100))
Kuri5u
发表于 2021-5-4 11:37:10
i= sum(range(1,101))**2
j=0
for each_num in range(1,101):
j+=each_num**2
print(i-j)
卢本伟牛逼
发表于 2021-8-9 00:27:47
#include <stdio.h>
int main(void)
{//时间复杂度O(1) 空间复杂度O(1)
int value = 100;
printf("result = %d", ((1+value)/2*value) * ((1+value)/2*value) - value*(value + 1)*(2 * value + 1)/6);
return 0;
}
鱼塘里的鱼儿
发表于 2021-10-7 21:36:44
#include <iostream>
using namespace std;
int main()
{
int sum1=0,sum2=0;
for(int i=1;i<21;i++)
{
sum1+=i*i;
sum2+=i;
if(i==20)
sum1=sum2*sum2;
}
cout<<sum1-sum2<<endl;
return 0;
}
用C++写的结果:43890
ft215378
发表于 2021-10-9 12:57:39
#(平方的和)与(和的平方) de 差 是多少
sum_square = 0
for i in range(100):
sum_square += i ** 2
print("前100个自然数的平方的和是%d" % sum_square)
square_sum = 0
for j in range(100):
square_sum += i
square_sum = square_sum ** 2
print("前100个自然数的和的平方是%d" % square_sum)
result = square_sum - sum_square
print("平方和与和的平方的差为%d" % result)
番杰
发表于 2021-10-27 14:16:28
#include<stdio.h>
#include<math.h>
int mian(void)
{
int result ;
unsigned int sum1 = 0,sum2 = 0;
for(int i = 1;i<101;i++)
{
sum1 += pow(i,2);
}
sum2 = 99 * 100 /2 + 100 ;
sum2 *= sum2;
result = sum2 - sum1;
printf("%d",result;
return 0;
}
梦雨
发表于 2021-12-9 13:42:05
#include<stdio.h>
#define max 100
int pow123(int a,int r)
{
int y=a;
for(int i=1; i<r; i++)
{
a*=y;
}
return a;
}
int main()
{
int r;
long ma=0,k=0;
int a;
int min;
for(int i=0; i<max; i++)
{
a=i+1;
}
for(int w=0; w<max; w++)//平方和and和的平方
{
r=pow123(a,2);
k+=r;
ma+=a;
}
min=pow123(ma,2);
printf("%d",min-k);
return 0;
}
Kazimierz
发表于 2022-2-22 16:27:43
#include <stdio.h>
int main()
{
int a=0,b=0,c=0,result=0,i;
for(i=1;i<=100;i++)
{
a+=i*i;
b+=i;
}
c=b*b;
result=c-a;
printf("%d",result);
return 0;
}
Asss-whom
发表于 2022-8-7 21:48:07
Rust + Rayon并行化暴算:
use rayon::prelude::*;
use std::time::Instant;
fn main() {
let now = Instant::now();
let num_pow: i32 = (1..101i32)
.into_par_iter()
.map(|x| x.pow(2))
.sum();
let num_sum: i32 = (1..101i32)
.into_par_iter()
.sum();
let num = num_sum.pow(2)- num_pow;
println!("{}", num);
println!("耗时{}毫秒。", now.elapsed().as_millis())
}
---
25164150
耗时0毫秒。
Eca
发表于 2023-4-6 20:20:04
#include <stdio.h>
int main() {
long long int pfh = 0, hpf = 0;
for (int i = 1; i <= 100; i++) {
pfh += i * i;
hpf += i;
}
printf("和平方-平方和 -> %lld 与 %lld 的差为: %lld", pfh, hpf * hpf,
(pfh > (hpf * hpf) ) ? pfh - (hpf * hpf) : (hpf * hpf) - pfh);
return 0;
}
感觉不够漂亮{:10_279:}
qingyunvi
发表于 2023-10-28 22:07:31
import time
def sum_of_squares(n):
total = 0
for i in range(n + 1):
total += i * i
return total
def root_sum_square(n):
num = sum(range(n+1))
return num * num
start = time.time()
val = root_sum_square(100) - sum_of_squares(100)
print(val)
end = time.time()
print("time: %s" % (end - start))
答案:25164150