-1

I'm working with factorials around 30! and I can't get a very good result for the value. I only want it accurate to 6 significant figures, or anything that gives a reasonable result. Currently my code is...

    static void Main(string[] args)
    {
        {
            int number = 30;
            long fact;
            factorial(number, out  fact);
        }
    }
    public static void factorial(int events, out  long  eventfact)
    {
        eventfact = 1;
        for (int tt = 1; tt <= events; tt++)
        {
            eventfact = eventfact * tt;
        }

    }

This returns the result

-8764578968847253504    

this is far from

2.65252859812191E+32

I've looked up about the different types of integers but nothing seems to be large enough for the numbers that I am working with.
Any help would be great!

Servy
  • 202,030
  • 26
  • 332
  • 449
Tim
  • 15
  • 5
  • 2
    I think you are looking for [BigInteger](https://msdn.microsoft.com/en-us/library/system.numerics.biginteger%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396). Also, consider using return statements rather than out parameters – Camilo Terevinto Jul 06 '17 at 16:56
  • Try using System.Numerics.BigInteger – The_Outsider Jul 06 '17 at 16:58
  • 1
    Company I work for provided search engine to look for answers - give it a try https://www.bing.com/search?q=c%23%20factorial%20big%20number... Results in suggestion to use library provided by the same company in language again by the same company...(Surprisingly using similar search on Google https://www.google.com/search?q=c%23%20factorial%20big%20number gives very similar results instead of suggesting Go or something else :)). – Alexei Levenkov Jul 06 '17 at 17:16

2 Answers2

3

You can use this library, you should not have any problem with 30! with this.

https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx

You can do something like ( I dont have a compiler here so)

using System.Numerics;
    static void Main(string[] args)
    {
        {
            int number = 30;
            long fact;
            factorial(number, out  fact);
        }
    }
    public static void factorial(int events, out  BigInteger eventfact)
    {
        eventfact = 1;
        for (int tt = 1; tt <= events; tt++)
        {
            eventfact = BigInteger.Multiply(eventfact, tt); }

    }
Luis Tellez
  • 2,785
  • 1
  • 20
  • 28
  • Wasn't me I don't have high enough repitation. Do you have any more of your code before the line `static void Main(string[] args)` ? I cant get BigInterger to work. – Tim Jul 06 '17 at 17:16
  • I know it was not you, it was other guy that just deleted the comment when he realized the timestamps... – Luis Tellez Jul 07 '17 at 11:01
  • You have to do the import, using System.Numerics; – Luis Tellez Jul 07 '17 at 11:02
1

You can use the BigInteger struct for this task.

static void Main(string[] args)
{
    int number = 30;
    BigInteger fact;
    factorial(number, out  fact);        
}

public static void factorial(int events, out BigInteger eventfact)
{
    eventfact = new BigInteger(1);
    for (int tt = 2; tt <= events; tt++)
    {
        eventfact = eventfact * tt;
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
KamikyIT
  • 314
  • 2
  • 15