鱼C论坛

 找回密码
 立即注册
楼主: 欧拉计划

题目6:平方和与和平方的差是多少?

[复制链接]
发表于 2022-2-22 16:27:43 | 显示全部楼层
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int a=0,b=0,c=0,result=0,i;
  5.        
  6.         for(i=1;i<=100;i++)
  7.         {
  8.                 a+=i*i;
  9.                 b+=i;
  10.         }
  11.        
  12.         c=b*b;
  13.         result=c-a;
  14.         printf("%d",result);
  15.        
  16.         return 0;
  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-7 21:48:07 | 显示全部楼层
Rust + Rayon并行化暴算:
  1. use rayon::prelude::*;
  2. use std::time::Instant;

  3. fn main() {
  4.     let now = Instant::now();
  5.     let num_pow: i32 = (1..101i32)
  6.     .into_par_iter()
  7.     .map(|x| x.pow(2))
  8.     .sum();
  9.     let num_sum: i32 = (1..101i32)
  10.     .into_par_iter()
  11.     .sum();
  12.     let num = num_sum.pow(2)- num_pow;
  13.     println!("{}", num);
  14.     println!("耗时{}毫秒。", now.elapsed().as_millis())
  15. }
复制代码

---
25164150
耗时0毫秒。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-6 20:20:04 | 显示全部楼层
  1. #include <stdio.h>

  2. int main() {
  3.         long long int pfh = 0, hpf = 0;
  4.         for (int i = 1; i <= 100; i++) {
  5.                 pfh += i * i;
  6.                 hpf += i;
  7.         }

  8.         printf("和平方-平方和   ->   %lld 与 %lld 的差为: %lld", pfh, hpf * hpf,
  9.                (pfh > (hpf * hpf) ) ? pfh - (hpf * hpf) : (hpf * hpf) - pfh);
  10.         return 0;
  11. }
复制代码

感觉不够漂亮
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-28 22:07:31 | 显示全部楼层
  1. import time


  2. def sum_of_squares(n):
  3.     total = 0
  4.     for i in range(n + 1):
  5.         total += i * i

  6.     return total


  7. def root_sum_square(n):
  8.     num = sum(range(n+1))
  9.     return num * num


  10. start = time.time()
  11. val = root_sum_square(100) - sum_of_squares(100)
  12. print(val)
  13. end = time.time()
  14. print("time: %s" % (end - start))
复制代码

答案:25164150
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-12 06:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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