from math import factorial
print(sum(ord(char) - 48 for char in str(factorial(100))))) 本帖最后由 永恒的蓝色梦想 于 2020-6-30 18:00 编辑
#include<stdio.h>
#define N 500
void main()
{
int sum = 0;//此变量用来计算统计结果各位数字之和
int count = 1;//用来统计计算结果的长度,也是fact数组被用了多少个元素
int i, j, carry = 0;//carry用来保存每次的进位
int fact = { 1 };//将fact初始化为1,方便后面的计算
for (i = 1; i <= 100; i++)
{
for (j = 0; j < count; j++)//手动模拟乘法
{
fact = fact * i + carry;//让fact数组每一位与i相乘,再加上进制位
carry = fact / 10;
fact %= 10;//fact数组的每一位数组都在0-9之间
}
while (carry > 0)//当for循环结束,最高为仍然可能产生进位,需要处理
{
fact = carry;
if (fact > 9)
{
carry = fact / 10;
fact %= 10;
}
else
{
carry = 0;
}
}
}
printf("100的阶乘结果为\n");
for (i = count - 1; i >= 0; i--)
printf("%d", fact);
printf("\n");
for (i = count - 1; i >= 0; i--)
sum += fact;
printf("100!的和 = %d\n", sum);
getchar();
}
输出结果为:
100的阶乘结果为
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
100!的和 = 648
import math as m
num = int (m.factorial(100))
n = str(num)
s = 0
for i in n:
s = s + int(i)
print (s) 本帖最后由 a1351468657 于 2021-3-8 19:44 编辑
我觉得这种大数运算用python真的没意思。我刚去用python写了下, 1分钟就写出来了。建议大家用C或者其他语言写下。 数据小随从 发表于 2020-6-26 20:44
输出结果为:
100的阶乘结果为
9332621544394415268169923885626670049071596826438162146859296389521 ...
牛! 好想法! 数据小随从 发表于 2020-6-26 20:44
输出结果为:
100的阶乘结果为
9332621544394415268169923885626670049071596826438162146859296389521 ...
while (carry)//当for循环结束,最高为仍然可能产生进位,需要处理
{
fact = carry % 10;
carry /= 10;
count++;
}
这处可以修改成这样, 应该效率更高 在这道题目开始感受到数据类型深深的恶意,python是真的爽{:10_260:} #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)
/*
答案:648
耗时:0.0000694秒
*/
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
struct stBigInt //作一个高精度整数类
{
short a;
stBigInt(void) //构造函数
{
memset(a, 0, sizeof(a));
}
stBigInt(short k)//构造函数
{
memset(a, 0, sizeof(a));
int i = 0;
while (k > 0)
{
a = k % 10;
k /= 10;
}
}
void operator*(int k)//数乘运算
{
short s = 0;
for (int i = 0; i < 192; ++i)
{
a = a * k;
a += s;
s = a;
a = s % 10;
s /= 10;
}
}
int GetSum(void)//取得各位数字之和
{
int nSum = 0;
for (int i = 0; i < 192; ++i)
nSum += a;
return nSum;
}
};
int main(void)
{
stBigInt x(1);
for (int i = 2; i <= 100; ++i)
x* i;
cout << x.GetSum() << endl;
return 0;
} import time as t
start = t.perf_counter()
def calc_factorial(num):
factorial = 1
for i in range(num):
factorial *= (i + 1)
return factorial
res = calc_factorial(100)
print(sum(int(j) for j in str(res)))
print("It costs %f s" % (t.perf_counter() - start)) use num::bigint::ToBigUint;
use std::time::Instant;
fn main() {
let now = Instant::now();
let mut value = 1.to_biguint().unwrap();
for i in 1..101u32 { value *= i }
let num: i32 = value
.to_string()
.bytes()
.map(|x| (x - 48) as i32)
.sum();
println!("cost {} ms.", now.elapsed().as_millis());
println!("{num}")
}
cost 0 ms.
648 本帖最后由 mathtimes 于 2023-3-30 22:57 编辑
#include <fmt/core.h>
#include <cstdint>
#include <vector>
#include <algorithm>
void mul(std::vector<char>& bits, uint64_t i) {
int carry = 0;
for (auto& j : bits) {
uint64_t var = uint64_t(j) * i + carry;
carry = var / 10;
j = var % 10;
}
while (carry) {
bits.push_back(carry % 10);
carry /= 10;
}
}
int main () {
auto bits = *new std::vector<char>{1};
for (uint64_t i = 2; i < 100; i++)
mul(bits, i);
uint64_t count = 0;
for (auto i : bits)
count += i;
fmt::print("{}\n", count);
return 0;
}
648
0.005816 seconds
页:
1
[2]