+ Reply to Thread
Results 1 to 6 of 6

Factorials of larger(er) numbers in Excel

  1. #1
    Registered User
    Join Date
    09-24-2018
    Location
    Philadelphia, PA
    MS-Off Ver
    365
    Posts
    2

    Factorials of larger(er) numbers in Excel

    Does anyone know how to calculate numbers larger than 179!, i.e. FACT(179)? Excel's number limit is pretty small when trying to do real-world statistics problems (10^308). In doing problems of this sort, often ratios of large factorials are required, but at the relatively low number cited Excel bombs out. The ratios are often small but the large number limit interferes with calculating them.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Factorials of larger(er) numbers in Excel

    You can use Stirling's Approximation to calculate the log of the factorial of large numbers:

    A
    B
    C
    1
    n
    170
    B1: Input
    2
    n!
    7.26E+306
    B2: =FACT(B1)
    3
    log(n!)
    306.86078
    B3: =LOG(B2)
    4
    ≈log(n!)
    306.86057
    B4: =LOG(2*PI()*B1) / 2 + B1*LOG(B1/EXP(1))
    5
    6
    n
    1,000
    B6: Input
    7
    n!
    #NUM!
    B7: =FACT(B6)
    8
    log(n!)
    #NUM!
    B8: =LOG(B7)
    9
    ≈log(n!)
    2567.6046
    B9: =LOG(2*PI()*B6) / 2 + B6*LOG(B6/EXP(1))
    10
    11
    n
    10,000,000
    B11: Input
    12
    n!
    #NUM!
    B12: =FACT(B11)
    13
    log(n!)
    #NUM!
    B13: =LOG(B12)
    14
    ≈log(n!)
    65657059
    B14: =LOG(2*PI()*B11) / 2 + B11*LOG(B11/EXP(1))
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,453

    Re: Factorials of larger(er) numbers in Excel

    1E308 is not just Excel's limit, but that is the standard limit for double precision float variables across programming languages (at least those that use standard IEEE like definitions for double precision floating point). And, since double precision stores only 15 digits of precision, any result above ~1E16 will exhibit rounding error.

    The real problem is that Excel (and all other spreadsheets as far as I know) only knows how to store numbers as double precision. My first recommendation is to seek out a different programming language, if you really need to work with these kinds of numbers. My limited search found:
    R appears to also be limited to double precision, but I found a few references to some external libraries that can extend to higher precision (maybe even arbitrary precision).
    Java has the BigInteger and BigDecimal data types that appear to be arbitrary precision.
    I have seen procedures for VBA that implement some arbitrary precision operations (add, substract, and multiply, but I have not seen a factorial function).
    I expect that other languages like Python or Mathcad/Mathematica will also have arbitrary precision algorithms available.

    Choices will depend on exactly how large of a number you need to compute and how many digits of precision you need.

    Wikipedia's article on arbitrary precision uses factorials as an example https://en.wikipedia.org/wiki/Arbitr...hmetic#Example where they talk about doing these calculations in log() space, or using what it calls Stirling's approximation (link in Wikipedia article also see shg's example in previous post). Pseudocode for an "arbitrary" precision algorithm is also given.

    How do you want to proceed?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Factorials of larger(er) numbers in Excel

    I'd also add my 5 cents

    Well, I'd say if you need exact result you are in problem already with 21!

    Note that true value is:
    51 090 942 171 709 440 000
    while, with numbers beeing represented in Excel as IEEE double precision floating point (ca 15 digits accuracy) FACT calculates it as
    51 090 942 171 709 400 000

    Now a bit better news if you shall stick to Excel:
    If you:
    1) can live with the accuracy limit, and
    2) need to calculate logarithms (quite often for large factorials)
    then you can use gamma distribution:
    =GAMMALN(n+1)
    which is equal to
    =ln(FACT(n))

    Of course you cannot calculate the latter for n>170 in excel because FACT(n) will return #NUM! error, but with GAMMALN you can go waaaaay further

    And if you need log(n!) not ln(n!) nothing easier than: =GAMMALN(n+1)/LN(10)
    Best Regards,

    Kaper

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Factorials of larger(er) numbers in Excel

    GammaLn, of course! Forgot about that.

  6. #6
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Factorials of larger(er) numbers in Excel

    Quote Originally Posted by Kaper View Post
    if you need exact result you are in problem already with 21!
    Actually, starting with FACT(23).

    By coincidence, Excel does return the correct exact values for FACT(21) and FACT(22).

    They only appear incorrect because Excel formats only the first 15 significant decimal digits (rounded).

    But the exact decimal representation of the internal binary values are 51090942171709440000 for FACT(21) and 1124000727777607680000 for FACT(22).

    Even with VBA type Decimal, we can calculate only up to FACT(27) exactly.

    There are Excel add-ins that claim to support much larger numbers. I have no experience with any of them. But if they can multiply arbitrarily large integers, you should be able to design your own FACT implementation. However, most such large numbers can only be represented accurately as text.

    But I agree with "MrShorty": if you need to work with such large numbers, Excel is not the right product for you. That doesn't mean that Excel is flawed; it just means it is not designed for that purpose. My Engish dictionary "fails" to show definitions for (most) German words. That does not mean its design is flawed. That's simply not its purpose.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to nest whole numbers in a larger number
    By peterrulezzz in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-22-2016, 09:40 AM
  2. Exponential value for larger numbers
    By gan_xl in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-27-2014, 12:28 PM
  3. Sum of the first n factorials
    By ssayani87 in forum Excel General
    Replies: 4
    Last Post: 09-04-2012, 12:55 PM
  4. Formula Coding for Factorials
    By alouie457 in forum Excel General
    Replies: 4
    Last Post: 11-12-2009, 06:52 PM
  5. Factorials
    By 240excel in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-06-2007, 11:40 AM
  6. [SOLVED] Pulling larger numbers from group
    By Smint in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 01:05 AM
  7. Larger Numbers!!!
    By exsam21 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-30-2005, 04:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1