鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 欧拉计划

题目20:算出100!的各位之和

[复制链接]
发表于 2019-8-3 20:47:53 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-7-31 17:40 编辑
  1. from math import factorial
  2. print(sum(ord(char) - 48 for char in str(factorial(100)))))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-26 20:44:53 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2020-6-30 18:00 编辑
  1. #include<stdio.h>
  2. #define N 500


  3. void main()
  4. {
  5.         int sum = 0;//此变量用来计算统计结果各位数字之和
  6.         int count = 1;//用来统计计算结果的长度,也是fact数组被用了多少个元素
  7.         int i, j, carry = 0;//carry用来保存每次的进位
  8.         int fact[N] = { 1 };//将fact[0]初始化为1,方便后面的计算
  9.         for (i = 1; i <= 100; i++)
  10.         {
  11.                 for (j = 0; j < count; j++)//手动模拟乘法
  12.                 {
  13.                         fact[j] = fact[j] * i + carry;//让fact数组每一位与i相乘,再加上进制位
  14.                         carry = fact[j] / 10;
  15.                         fact[j] %= 10;//fact数组的每一位数组都在0-9之间
  16.                 }
  17.                 while (carry > 0)//当for循环结束,最高为仍然可能产生进位,需要处理
  18.                 {
  19.                         fact[count++] = carry;
  20.                         if (fact[count - 1] > 9)
  21.                         {
  22.                                 carry = fact[count - 1] / 10;
  23.                                 fact[count - 1] %= 10;

  24.                         }
  25.                         else
  26.                         {
  27.                                 carry = 0;
  28.                         }
  29.                 }
  30.         }
  31.         printf("100的阶乘结果为\n");
  32.         for (i = count - 1; i >= 0; i--)
  33.                 printf("%d", fact[i]);
  34.         printf("\n");
  35.         for (i = count - 1; i >= 0; i--)
  36.                 sum += fact[i];
  37.         printf("100!的和 = %d\n", sum);
  38.         getchar();
  39. }
复制代码

输出结果为:
100的阶乘结果为
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
100!的和 = 648
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-11-6 14:18:51 | 显示全部楼层

import math as m

num = int (m.factorial(100))

n = str(num)
s = 0
for i in n:
    s = s + int(i)

print (s)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-8 18:38:46 | 显示全部楼层
本帖最后由 a1351468657 于 2021-3-8 19:44 编辑

我觉得这种大数运算用python真的没意思。我刚去用python写了下, 1分钟就写出来了。建议大家用C或者其他语言写下。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-8 19:44:00 | 显示全部楼层
数据小随从 发表于 2020-6-26 20:44
输出结果为:
100的阶乘结果为
9332621544394415268169923885626670049071596826438162146859296389521 ...

牛! 好想法!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-8 20:31:00 | 显示全部楼层
数据小随从 发表于 2020-6-26 20:44
输出结果为:
100的阶乘结果为
9332621544394415268169923885626670049071596826438162146859296389521 ...

while (carry)//当for循环结束,最高为仍然可能产生进位,需要处理
                {
                        fact[count] = carry % 10;
                        carry /= 10;
                        count++;
                }
这处可以修改成这样, 应该效率更高
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-9 00:54:07 | 显示全部楼层
在这道题目开始感受到数据类型深深的恶意,python是真的爽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-14 21:56:11 | 显示全部楼层
#100的阶乘各位和
#求阶乘
num = 100
result = 1
while num:
    result *= num
    num -= 1
#求和
s = str(result)
sum = 0
for i in s:
    sum += int(i)
print(sum)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-2 19:43:04 | 显示全部楼层
  1. /*
  2. 答案:648
  3. 耗时:0.0000694秒
  4. */
  5. #include <iostream>
  6. #include <cstring>
  7. #include <cmath>
  8. using namespace std;

  9. struct stBigInt //作一个高精度整数类
  10. {
  11.   short a[192];
  12.   stBigInt(void) //构造函数
  13.   {
  14.     memset(a, 0, sizeof(a));
  15.   }
  16.   stBigInt(short k)//构造函数
  17.   {
  18.     memset(a, 0, sizeof(a));
  19.     int i = 0;
  20.     while (k > 0)
  21.     {
  22.       a[i++] = k % 10;
  23.       k /= 10;
  24.     }
  25.   }
  26.   void operator*(int k)//数乘运算
  27.   {
  28.     short s = 0;
  29.     for (int i = 0; i < 192; ++i)
  30.     {
  31.       a[i] = a[i] * k;
  32.       a[i] += s;
  33.       s = a[i];
  34.       a[i] = s % 10;
  35.       s /= 10;
  36.     }
  37.   }
  38.   int GetSum(void)//取得各位数字之和
  39.   {
  40.     int nSum = 0;
  41.     for (int i = 0; i < 192; ++i)
  42.       nSum += a[i];
  43.     return nSum;
  44.   }
  45. };

  46. int main(void)
  47. {
  48.   stBigInt x(1);
  49.   for (int i = 2; i <= 100; ++i)
  50.     x* i;
  51.   cout << x.GetSum() << endl;
  52.   return 0;
  53. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-8 16:05:33 | 显示全部楼层
  1. import time as t

  2. start = t.perf_counter()


  3. def calc_factorial(num):
  4.     factorial = 1
  5.     for i in range(num):
  6.         factorial *= (i + 1)
  7.     return factorial


  8. res = calc_factorial(100)
  9. print(sum(int(j) for j in str(res)))
  10. print("It costs %f s" % (t.perf_counter() - start))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-8 12:28:41 | 显示全部楼层
  1. use num::bigint::ToBigUint;
  2. use std::time::Instant;

  3. fn main() {
  4.     let now = Instant::now();
  5.     let mut value = 1.to_biguint().unwrap();
  6.     for i in 1..101u32 { value *= i }
  7.     let num: i32 = value
  8.         .to_string()
  9.         .bytes()
  10.         .map(|x| (x - 48) as i32)
  11.         .sum();
  12.     println!("cost {} ms.", now.elapsed().as_millis());
  13.     println!("{num}")
  14. }
复制代码

cost 0 ms.
648
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-30 22:55:55 | 显示全部楼层
本帖最后由 mathtimes 于 2023-3-30 22:57 编辑
  1. #include <fmt/core.h>
  2. #include <cstdint>
  3. #include <vector>
  4. #include <algorithm>

  5. void mul(std::vector<char>& bits, uint64_t i) {
  6.     int carry = 0;
  7.     for (auto& j : bits) {
  8.         uint64_t var = uint64_t(j) * i + carry;
  9.         carry = var / 10;
  10.         j = var % 10;
  11.     }
  12.     while (carry) {
  13.         bits.push_back(carry % 10);
  14.         carry /= 10;
  15.     }
  16. }

  17. int main () {
  18.     auto bits = *new std::vector<char>{1};
  19.     for (uint64_t i = 2; i < 100; i++)
  20.         mul(bits, i);
  21.     uint64_t count = 0;
  22.     for (auto i : bits)
  23.         count += i;
  24.     fmt::print("{}\n", count);
  25.     return 0;
  26. }
复制代码


648
0.005816 seconds
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 00:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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