Left factorials: Difference between revisions
m (added "entry" to "see also:".) |
m (added an extra blank line for the formula (to read better).) |
||
Line 11: | Line 11: | ||
This Rosetta Code task will be using the formula for ''left factorial'': |
This Rosetta Code task will be using the formula for ''left factorial'': |
||
<br> |
<br><br> |
||
::::: <math> !n = \sum_{k=0}^{n-1} k! </math> |
::::: <math> !n = \sum_{k=0}^{n-1} k! </math> |
||
Revision as of 21:10, 29 March 2014
Left factorials have a confusing name as it can refer to:
- subfactorials: !n
- factorial sums: !n
and one can see the exact notation being used for both.
Sometimes, subfactorials use the notation: !n` or !n' or n¡.
(Subfactorials are also known as derangements.)
This Rosetta Code task will be using the formula for left factorial:
- where
- !0 = 0
- task requirements
Display the left factorials for:
- zero through ten (inclusive)
- 20 through 110 (inclusive) by tens
Display the length (in decimal digits) of the left factorials for:
- 1,000 through 10,000 (inclusive) by thousands
- Also see
- The OEIS entry: [A003422 left factorials] in The On-Line Encyclopedia of Integer Sequences (R).
- The MathWorld (TM) entry: [left factorial] in Wolfram MathWorld (TM).
- The MathWorld (TM) entry: [factorial sums] in Wolfram MathWorld (TM).
- The MathWorld (TM) entry: [subfactorial] in Wolfram MathWorld (TM).
REXX
<lang rexx>/*REXX pgm computes/shows the left factorial (or width) of N (or range).*/ parse arg bot top inc . /*obtain optional args from C.L. */ if bot== then bot=1 /*BOT defined? Then use default.*/ td= bot<0 /*if BOT < 0, only show # digs.*/ bot=abs(bot) /*use the |bot| for the DO loop.*/ if top== then top=bot /* " " top " " " " */ if inc= then inc=1 /* " " inc " " " " */ @='left ! of ' /*a literal used in the display. */ w=length(H) /*width of largest number request*/
do j=bot to top by inc /*traipse through #'s requested.*/ if td then say @ right(j,w) " ───► " length(L!(j)) ' digits' else say @ right(j,w) " ───► " L!(j) end /*j*/ /* [↑] show either L! or #digits*/
exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────L! subroutine───────────────────────*/ L!: procedure; parse arg x .; if x<3 then return x; s=4 /*shortcuts.*/ !=2; do f=3 to x-1 /*compute L! for all numbers───►X*/
!=!*f /*compute intermediate factorial.*/ if pos(.,!)\==0 then numeric digits digits()*1.5%1 /*bump digs.*/ s=s+! /*add the factorial ───► L! sum.*/ end /*f*/ /* [↑] handles gi-hugeic numbers*/
return s /*return the sum (L!) to invoker.*/</lang> output when using the input: 0 10
left ! of 0 ───► 0 left ! of 1 ───► 1 left ! of 2 ───► 2 left ! of 3 ───► 4 left ! of 4 ───► 10 left ! of 5 ───► 34 left ! of 6 ───► 154 left ! of 7 ───► 874 left ! of 8 ───► 5914 left ! of 9 ───► 46234 left ! of 10 ───► 409114
output when using the input: 20 110 10
left ! of 20 ───► 128425485935180314 left ! of 30 ───► 9157958657951075573395300940314 left ! of 40 ───► 20935051082417771847631371547939998232420940314 left ! of 50 ───► 620960027832821612639424806694551108812720525606160920420940314 left ! of 60 ───► 141074930726669571000530822087000522211656242116439949000980378746128920420940314 left ! of 70 ───► 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314 left ! of 80 ───► 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314 left ! of 90 ───► 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314 left ! of 100 ───► 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314 left ! of 110 ───► 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
output when using the input: -1000 10000 1000
left ! of 1000 ───► 2565 digits left ! of 2000 ───► 5733 digits left ! of 3000 ───► 9128 digits left ! of 4000 ───► 12670 digits left ! of 5000 ───► 16322 digits left ! of 6000 ───► 20062 digits left ! of 7000 ───► 23875 digits left ! of 8000 ───► 27749 digits left ! of 9000 ───► 31678 digits left ! of 10000 ───► 35656 digits