大数的阶乘总和

时间:2011-08-01 22:34:16

标签: algorithm math factorial sum-of-digits

我想计算N!的数字总和。

我想为N的真正大值做这个,比如说N(1500)。我没有使用.NET 4.0。我无法使用BigInteger类来解决这个问题。

这可以通过其他算法或程序解决吗?请帮忙。

我想做一些像Calculate the factorial of an arbitrarily large number, showing all the digits这样的事情但是在C#中。但是我无法解决。

7 个答案:

答案 0 :(得分:1)

就我而言,没有特殊的魔法允许你计算数字的总和。

无论如何创建自己的BigInteger类并不困难 - 你只需要实现3年级的长乘法算法。

答案 1 :(得分:1)

如果您的目标是计算N!的数字之和,并且N是合理限制的,则可以在没有BigInteger类型的情况下执行以下操作:

  • 在线查找因子值列表(表查找比从头开始计算更有效,并且不需要BigInteger
  • 以字符串数据类型存储
  • 将字符串中的每个字符解析为整数
  • 添加生成的整数

答案 2 :(得分:1)

有两种性能快捷方式可用于您选择的任何实现。

  1. 从数字中删除任何零。
  2. 如果数字可被5 ^ n整除,则除以10 ^ n。
  3. 这样,

    16*15*14*13*12*11*10*9*8*7*6*5*4*3*2 = 20,922,789,888,000
    //-->
    16*1.5*14*13*12*11*1*9*8*7*6*0.5*4*3*2 = 20,922,789,888 //Sum of 63
    

    此外,感觉应该有一些算法而不回复计算全部。转到18 !,数字的总和是:

    2,6,6,3,9,9,9,27,27,36,27,27,45,45,63,63,63
    //the sums of the resulting digits are:
    2,6,6,3,9,9,9,9,9,9,9,9,9,9,9,9,9
    

    值得注意的是,1500的数字之和!是16749(其数字之和为27)

答案 3 :(得分:0)

这是一些有效的代码。可以改进一些组件以提高效率。我的想法是使用我在学校告诉的任何乘法算法,并将长整数存储为字符串。

作为事后的想法,我认为用List<int>()代替string表示大数字更为明智。但我会将其作为练习留给读者。

代码示例

static string Mult(string a, string b)
    {
        int shift = 0;
        List<int> result = new List<int>();
        foreach (int aDigit in a.Reverse().Select(c => int.Parse(c.ToString())))
        {
            List<int> subresult = new List<int>();
            int store = 0;
            foreach (int bDigit in b.Reverse().Select(c => int.Parse(c.ToString())))
            {
                int next = aDigit*bDigit + store;
                subresult.Add(next%10);
                store = next/10;
            }

            if (store != 0) subresult.Add(store);

            subresult.Reverse();
            for (int i = 0; i < shift; ++i) subresult.Add(0);
            subresult.Reverse();

            int newResult = new List<int>();
            store = 0;
            for (int i = 0; i < subresult.Count; ++i)
                {
                    if (result.Count >= i + 1)
                    {
                        int next = subresult[i] + result[i] + store;
                        if (next >= 10)
                            newResult.Add(next % 10);
                        else newResult.Add(next);
                        store = next / 10;
                    }
                    else
                    {
                        int next = subresult[i] + store;
                        newResult.Add(next % 10);
                        store = next / 10;
                    }
                }

            if (store != 0) newResult.Add(store);

            result = newResult;
            ++shift;
        }

        result.Reverse();
        return string.Join("", result);
    }

    static int FactorialSum(int n)
    {
        string result = "1";
        for (int i = 2; i <= n; i++)
            result = Mult(i.ToString(), result);
        return result.Sum(r => int.Parse(r.ToString()));
    }

代码测试

假设上面的代码段与您的Main方法属于同一个类,请立即调用它。

<强>输入

    static void Main(string[] args)
    {
        Console.WriteLine(FactorialSum(1500));
    }

<强>输出

16749

答案 4 :(得分:0)

这是您在其中一条评论中引用的the C++ code端口。从C ++移植到C#时要注意的一件事是,零值的整数计算为false,非零的整数在布尔比较中使用时评估为true。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArbitraryFactorial
{
    class Program
    {
        const int max = 5000;

        static void display(int[] arr)
        {
            int ctr = 0;
            for (int i = 0; i < max; i++)
            {
                if (ctr == 0 && arr[i] != 0) ctr = 1;
                if (ctr != 0)
                    Console.Write(arr[i]);

            }
        }

        static void factorial(int[] arr, int n)
        {
            if (n == 0) return;
            int carry = 0;
            for (int i = max - 1; i >= 0; --i)
            {
                arr[i] = (arr[i] * n) + carry;
                carry = arr[i] / 10;
                arr[i] %= 10;
            }
            factorial(arr, n - 1);
        }

        static void Main(string[] args)
        {
            int[] arr = new int[max];
            arr[max - 1] = 1;
            int num;
            Console.Write("Enter the number: ");
            num = int.Parse(Console.ReadLine());
            Console.Write("Factorial of " + num + " is: ");
            factorial(arr, num);
            display(arr);
        }
    }
}

答案 5 :(得分:0)

您可以在http://codingloverlavi.blogspot.in/2013/03/here-is-one-more-interesting-program.html

找到源代码
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<time.h>
#define max 5000
void multiply(long int *,long int);
void factorial(long int *,long int);


int main()
{
clrscr();
cout<<"PROGRAM TO CALCULATE FACTORIAL OF A NUMBER";
cout<<"\nENTER THE NUMBER\n";
long int num;
cin>>num;

long int a[max];
for(long int i=0;i<max;i++)
    a[i]=0;

factorial(a,num);

clrscr();

//PRINTING THE FINAL ARRAY...:):):)
cout<<"THE FACTORIAL OF "<<num<<" is "<<endl<<endl;
long int flag=0;

int ans=0;
for(i=0;i<max;i++)
{
    if(flag||a[i]!=0)
    {
        flag=1;
        cout<<a[i];
        ans=ans+a[i];
    }
}

cout<<endl<<endl<<"the sum of all digits is: "<<ans;


getch();
return 1;
}

void factorial(long int *a,long int n)
{
long int lavish;
long int num=n;
lavish=n;
for(long int i=max-1;i>=0&&n;i--)
{
    a[i]=n%10;
    n=n/10;
}

for(i=2;i<(lavish);i++)
{
    multiply(a,num-1);
    num=num-1;

}
}


void multiply(long int *a,long int n)
{

for(long int i=0;i<max;i++)
    a[i]=a[i]*n;

for(i=max-1;i>0;i--)
{
    a[i-1]=a[i-1]+(a[i]/10);
    a[i]=a[i]%10;
}
}

答案 6 :(得分:-1)

如果没有BigInteger类型,则无法使用这些数字 没有算法或程序可以将大于2 64 的数字压缩到long

您需要为.Net 3.5找到BigInteger实现。