计算在100阶乘的数字的总和 [英] Calculate the sum of digits in 100 factorial

查看:147
本文介绍了计算在100阶乘的数字的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑 - 更改标题,以匹配实际问题陈述

Edit - Changed title to match the actual problem statement.

我的编程,计算在100的数字之和功能!但我似乎有两个大的问题。

I'm programming a function that calculates the sum of digits in 100! but I seem to be having two big issues.


  1. 100,实际的结果!只有精确到第几号(实际结果是93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000)

  1. The actual result of 100! is only accurate to the first few numbers (actual result is 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000)

我对相加得到的数字的位数的方法不输出正确的结果。

My method for adding up the digits of the resulting number doesn't output the correct result.

这是我目前的code:

This is my current code:

void factorialSum()
{
    double fact100 = factorial(100);
    double suma = 0;

    printf("100! is equal to: %.0f", fact100);

    while (fact100 > 0)
    {
        double temporal = fmod(fact100, 10);
        suma = suma + temporal;
        fact100 = fact100/10;
    }
    printf("\nThe sum of all digits in 100! is: %.0f", suma);
}

和功能阶乘()被定义为:

And the function factorial() is defined as:

double factorial (double n)
{
    double mult = 1;
    double i = n;

    while (i>=1)
    {
        mult *= i;
        i = i - 1;
    }
    return mult;
}

方案产出93326215443944102188325606108575267240944254854960571509166910400407995064242937148632694030450512898042989296944474898258737204311236641477561877016501813248作为100的结果!并说其数字的总和等于666。

The program outputs 93326215443944102188325606108575267240944254854960571509166910400407995064242937148632694030450512898042989296944474898258737204311236641477561877016501813248 as a result for 100! and says the summation of its digits is equal to 666.

任何帮助是AP preciated,谢谢你。

Any help is appreciated, thank you.

推荐答案

在C,一个双击通常有precision,相当于16的53位或17位precision的。所以一旦你超越 22 <!/ code>,一个双击不再重新present确切的结果,证明了通过以下code。请注意,在 23 <!/ code>中,尾随零消失,因为双击不再重新presents的精确值

In C, a double typically has 53 bits of precision, which corresponds to 16 or 17 digits of precision. So once you go beyond 22!, a double can no longer represent the exact result, as demonstrated by the following code. Note that at 23!, the trailing zeros disappear, since the double no longer represents the exact value.

#include <stdio.h>
#include <stdint.h>

int main( void )
{
    double y;

    y = 1;
    for ( int i = 2; i < 30; i++ )
    {
        y *= i;
        printf( "%2d %32.0lf\n", i, y );
    }
}

下面是从程序的输出

 2                                2
 3                                6
 4                               24
 5                              120
 6                              720
 7                             5040
 8                            40320
 9                           362880
10                          3628800
11                         39916800
12                        479001600
13                       6227020800
14                      87178291200
15                    1307674368000
16                   20922789888000
17                  355687428096000
18                 6402373705728000
19               121645100408832000
20              2432902008176640000
21             51090942171709440000
22           1124000727777607680000
23          25852016738884978212864
24         620448401733239409999872
25       15511210043330986055303168
26      403291461126605650322784256
27    10888869450418351940239884288
28   304888344611713836734530715648
29  8841761993739700772720181510144

如果要计算的 100 <!/ code>你需要使用数字数组(又名的大数的),做计算。你可以找到一个BIGNUM库使用,或者自己实现BIGNUM倍增。在大数维基百科的文章提供了伪code 阶乘。

If you want to compute the exact value of 100! you need to use arrays of digits (aka bignums), to do the calculations. You can either find a bignum library to use, or implement bignum multiplication yourself. The wikipedia article on bignums provides the pseudocode for calculating factorials.

这篇关于计算在100阶乘的数字的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆