Post Reply 
Mini-challenge - accurate factorials
07-07-2023, 08:40 PM
Post: #1
Mini-challenge - accurate factorials
.
A factorial, in decimal, will generally have a number of trailing zeros. For example
7! = 5040
has 4 digits in total, one trailing zero, and so we can say it has 3 significant digits.

So, given a calculator with some number of digits it can display, we might ask what's the largest factorial that will fit? (For example, consider calculators with 8, 10, 12, 14 digit displays, we ask what value of n would give rise to the large displayable n!)

And, given a calculator which has a scientific notation, such that the trailing zeros can be implicit, and needing only to display the significant digits, we can display even larger factorials. How large is the largest n, such that n! is accurately displayed with 8, 10, 12 or 14 significant digits?

Have fun!
Find all posts by this user
Quote this message in a reply
07-08-2023, 02:02 PM (This post was last modified: 07-08-2023 02:27 PM by J-F Garnier.)
Post: #2
RE: Mini-challenge - accurate factorials
(07-07-2023 08:40 PM)EdS2 Wrote:  given a calculator which has a scientific notation, such that the trailing zeros can be implicit, and needing only to display the significant digits, we can display even larger factorials. How large is the largest n, such that n! is accurately displayed with 8, 10, 12 or 14 significant digits?

If by accurate you mean exact, the largest exact factorials we can get are:
on a 10-digit machine (e.g. 15C):
    15! = 1307674368000 displayed as 1.307674368e12
on a 12-digit machine (e.g. 32S):
    17! = 355687428096000 displayed as 3.55687428096e14

Now, if we understand accurate as correctly rounded, then several machines ensure that all factorials are accurate (that is the error is less than 0.5 ULP).
For instance, the 71B, the 28S, the series 48.

But it's not true for some machines, for instance the 22S/32S/32SII:
    159! = 2.94670227249e282 (correct value is 2.94670227250e282)
Could you find the other three incorrectly rounded cases on these machines?
BTW, the 35S gives all correct values :-)

J-F
Visit this user's website Find all posts by this user
Quote this message in a reply
07-08-2023, 03:15 PM
Post: #3
RE: Mini-challenge - accurate factorials
Thanks - I did mean exact. But I, for one, welcome your extra challenge.
Find all posts by this user
Quote this message in a reply
07-08-2023, 03:23 PM (This post was last modified: 07-08-2023 03:24 PM by AndiGer.)
Post: #4
RE: Mini-challenge - accurate factorials
(07-08-2023 02:02 PM)J-F Garnier Wrote:  But it's not true for some machines, for instance the 22S/32S/32SII:
    159! = 2.94670227249e282 (correct value is 2.94670227250e282)

Interesting. As I'm not very familiar with the 32S I programmed a small routine to calculate factorials. Result: 159! = 2.94670227254e282

Google is your friend and so I found where to calculate with the built-in function.

Andi
Find all posts by this user
Quote this message in a reply
07-08-2023, 05:43 PM (This post was last modified: 07-08-2023 05:48 PM by John Keith.)
Post: #5
RE: Mini-challenge - accurate factorials
Code:

Mild spoilers...
.
.
.
.
.
.
.
.
.
.
[url=https://oeis.org/A034886]A034886[/url]- Number of digits in n!.

[url=https://oeis.org/A004154]A004154[/url]- Omit trailing zeros from factorial numbers.

There are formulas to compute these values but for the small numbers we are concerned with it is easier to just look them up. :)

As an aside, the HP 49 and 50 in Exact mode will give the exact value for n! limited only by the number of digits that will fit in memory. For the 49G+ and 50, the program FFAC is substantially faster than the built-in factorial function. On my HP 50, 1000! (2568 digits) takes about 29 seconds, whereas 1000 FFAC takes about 380 milliseconds.
Find all posts by this user
Quote this message in a reply
07-08-2023, 06:55 PM (This post was last modified: 07-08-2023 07:08 PM by DA74254.)
Post: #6
RE: Mini-challenge - accurate factorials
WP43 can do 40!
Which displays 815915283247897734345611269596115894272e9 where the last trailing 9 0's are seen in "show", thus it can display the 39 digits before all the trailing 0's.

Full number in "show" is: 815915283247897734345611269596115894272000000000

(If I understood your challenge right).

Esben
28s, 35s, 49G+, 50G, Prime G2 HW D, SwissMicros DM42, DM32, WP43 Pilot
Elektronika MK-52 & MK-61
Find all posts by this user
Quote this message in a reply
07-09-2023, 06:02 AM
Post: #7
RE: Mini-challenge - accurate factorials
Yes, that's what I was getting at - whether or not we choose to do SHOW changes the conditions a bit of course.

Similarly, it looks like Free42 can give us 36! - all the significant digits show up in SHOW mode, whereas 37! is too big and misses a few.

Thanks for the OEIS entries John!
Find all posts by this user
Quote this message in a reply
07-09-2023, 08:06 AM (This post was last modified: 07-09-2023 08:29 AM by J-F Garnier.)
Post: #8
RE: Mini-challenge - accurate factorials
(07-08-2023 06:55 PM)DA74254 Wrote:  WP43 can do 40!
Which displays 815915283247897734345611269596115894272e9

(07-09-2023 06:02 AM)EdS2 Wrote:  Yes, that's what I was getting at - whether or not we choose to do SHOW changes the conditions a bit of course.

Similarly, it looks like Free42 can give us 36! - all the significant digits show up in SHOW mode, whereas 37! is too big and misses a few.

Yes, 36! is exact in Free42, but surprisingly 40! =
8.159152832478977343456112695961164e47
has 5 ULP error.
That's a lot for just 4 additional multiplications, unless Free42 doesn't use guard digits in the factorial code, which I can't believe.

Edit: even more weird (on Free42):
39! = 2.039788208119744335864028173990290e46 (correct)
39! * 40 = 8.159152832478977343456112695961160e47 (just 1 ULP error, that's normal here).
So what is going on with the direct 40! calculation ?

J-F
Visit this user's website Find all posts by this user
Quote this message in a reply
07-09-2023, 11:04 AM
Post: #9
RE: Mini-challenge - accurate factorials
(07-07-2023 08:40 PM)EdS2 Wrote:  .
A factorial, in decimal, will generally have a number of trailing zeros. For example
7! = 5040
has 4 digits in total, one trailing zero, and so we can say it has 3 significant digits.

The above factorial has four significant digits since the trailing zero is not to the right of a decimal.

You can say that a factorial has zeros as the least significant digits, like 5040.

Namir
Find all posts by this user
Quote this message in a reply
07-09-2023, 11:25 AM
Post: #10
RE: Mini-challenge - accurate factorials
(Yes, I could and perhaps should have chosen a different adjective. Allow me temporarily to be Humpty Dumpty, for whom a word means exactly what it's intended to mean.)
Find all posts by this user
Quote this message in a reply
07-09-2023, 05:15 PM (This post was last modified: 07-13-2023 06:09 AM by Werner.)
Post: #11
RE: Mini-challenge - accurate factorials
(07-09-2023 08:06 AM)J-F Garnier Wrote:  Yes, 36! is exact in Free42, but surprisingly 40! =
8.159152832478977343456112695961164e47
has 5 ULP error.
That's a lot for just 4 additional multiplications, unless Free42 doesn't use guard digits in the factorial code, which I can't believe.

Edit: even more weird (on Free42):
39! = 2.039788208119744335864028173990290e46 (correct)
39! * 40 = 8.159152832478977343456112695961160e47 (just 1 ULP error, that's normal here).
So what is going on with the direct 40! calculation ?

J-F
Hi J-F,
Free42 does not use guard digits, since the intel decimal library has no provisions for extended precision for the 34-digit format.
The factorial is likely calculated as 40*39*..*2, not as 2*3*4..*39*40. Doing it manually shows the results you obtained.

Cheers, Werner
(that's *nine* full hours Bob! I'm slipping)

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
07-09-2023, 11:11 PM
Post: #12
RE: Mini-challenge - accurate factorials
(07-09-2023 05:15 PM)Werner Wrote:  Hi J-F,
Free42 does not use guard digits, since the intel decimal library has no provisions for extended precision for the 34-digit format.
The factorial is likely calculated as 40*39*..*2, not as 2*3*4..*39*40. Doing it manually show the results you obtained.

Cheers, Werner
(that's *nine* full hours Bob! I'm slipping)

Nah, no slipping here... it's all about quality. You're doing just fine... Wink

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
07-11-2023, 08:39 AM
Post: #13
RE: Mini-challenge - accurate factorials
(07-09-2023 05:15 PM)Werner Wrote:  Free42 does not use guard digits, since the intel decimal library has no provisions for extended precision for the 34-digit format.
The factorial is likely calculated as 40*39*..*2, not as 2*3*4..*39*40. Doing it manually show the results you obtained.

Thanks Werner, I came to the same conclusion.
So not a problem as such, but we need to keep in mind that not all 34 digits are correct.

Most of the time, the error is just a few ULPs, but I found some worst cases:

 61! = 5.075802138772247988008568121766260e83  (correct: 5.07...252e83  : 8 ULPs)
101! = 9.425947759838359420851623124482910e159 (correct: 9.42...937e159 : 27 ULPs)
251! = 8.114469214881860408125244525581122e494 (correct: 8.11...165e494 : 43 ULPs)

J-F
Visit this user's website Find all posts by this user
Quote this message in a reply
07-12-2023, 09:23 PM (This post was last modified: 07-12-2023 09:53 PM by Gilles.)
Post: #14
RE: Mini-challenge - accurate factorials
Brut force search in NewRPL :

Fact0:
Code:
 « → p
  « 800 1 FOR 'n'
      2000 SETPREC n !
      DUP XPON 1 + ALOG 
      IF DUP2 / p SETPREC UNROT / == THEN
        2000 SETPREC n n ! 2 →LIST EXIT
      END
    -1 STEP
  »
 »

Nota about NewRPL:
- SETPREC : set the number of significant digits. The default is 32 digits of precision. The maximum is 2000 digits of precision (for comparaison, the stock 50g ROM has a fixed precision of 12 digits displayed, 15 digits internally)
- EXIT : Exit the current loop

Code:
 8 Fact0 -> { 13 6227020800 }
10 Fact0 -> { 15 1307674368000 }
12 Fact0 -> { 17 355687428096000 }
14 Fact0 -> { 18 6402373705728000 }
32 Fact0 -> { 34 295232799039604140847618609643520000000 }
64 Fact0 -> { 57 405269195048772167556806019054323221349803847962266021451844812800000000000​00 }
128 Fact0 -> { 96 991677934870949689209571401541893801158183648651267795444376054838492222809​091499987689476037000748982075094738965754305639874560000000000000000000000
}
256 Fact0 -> { 165 542391066613158877498449501421284184382157203794136983425672691311657301726​04765680403290849565015553604650354393935095487058155225915554489271207416431263​90781922570357408851928637648701404640994397662139185673022050034907694293331407​7895545443758280540160000000000000000000000000000000000000000
}
512 Fact0 -> { 286 870689172796271554439702119838710809861359436384882148385757313579329580990​40337765606647296919822085262226784396179645422058535285013494774076543181031928​21521975674207518165853340117627024513062774476157616015873220009335948079806785​94020502008659592570378865714219286067426896030852695623349485776172755222069040​92744787035270291650252110759960256346897987385736293274187417570994046062830362​96662425273776049359170782217763779902901820404689866285801647374451120503197165​64288841092540076553621682557812736000000000000000000000000000000000000000000000​0000000000000000000000000
}
1024 Fact0 -> { 505 392861868403666344606434511328949851159761646778169500967877826152340954987​49694266874140124572723925272264103605631433461353773166349016542057566907963786​79369789249678066704827473997936383766298678185353503511636963690997288463459315​28349258044609104905102916225942689582472014503580935442476249977961703368048074​25452485517882532734515489150091355990925022044091523930368535184941914015837544​76432018854163989906304863360776309166418653769241591724992916135519350759876754​98801135317120524095642761462529462342291827527249923279305068492040834131194681​27534608220959477583691774158634323794314270352760496796509530793277138704284826​10530819410914572674844721261068071609184677061435087917832837620234857996721204​74765964351091187746565958503161362537259409187576449045156060884458132656558714​49737037124601474017541148483331297537869946687627144931121011685292261820754558​60292246216102026181045566962485992305361055397280927264396098934493316949836471​39866362012104395551155850035104037632095520211956841549686969991168000000000000​00000000000000000000000000000000000000000000000000000000000000000000000000000000​000000000000000000000000000000000
}
Find all posts by this user
Quote this message in a reply
07-13-2023, 07:35 AM
Post: #15
RE: Mini-challenge - accurate factorials
Nice! And thanks everyone, hope you had fun.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)