Special factorials: Difference between revisions

Content added Content deleted
(add RPL)
(→‎{{header|REXX}}: complete rewrite)
Line 1,693: Line 1,693:


=={{header|REXX}}==
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program to compute some special factorials: superfactorials, hyperfactorials,*/
<syntaxhighlight lang="rexx">
/* REXX program to calculate Special factorials */
/*───────────────────────────────────── alternating factorials, exponential factorials.*/
numeric digits 1000 /*allows humongous results to be shown.*/
call hdr 'super'; do j=0 to 9; $= $ sf(j); end; call tell
call hdr 'hyper'; do j=0 to 9; $= $ hf(j); end; call tell
call hdr 'alternating '; do j=0 to 9; $= $ af(j); end; call tell
call hdr 'exponential '; do j=0 to 5; $= $ ef(j); end; call tell
@= 'the number of decimal digits in the exponential factorial of '
say @ 5 " is:"; $= ' 'commas( efn( ef(5) ) ); call tell
@= 'the inverse factorial of'
do j=1 for 10; say @ right(!(j), 8) " is: " rf(!(j))
end /*j*/
say @ right(119, 8) " is: " rf(119)
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: procedure; parse arg x; != 1; do #=2 to x; != ! * #; end; return !
af: procedure; parse arg x; if x==0 then return 0; prev= 0; call af!; return !
af!: do #=1 for x; != !(#) - prev; prev= !; end; return !
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
ef: procedure; parse arg x; if x==0 | x==1 then return 1; return x**ef(x-1)
efn: procedure; parse arg x; numeric digits 9; x= x; parse var x 'E' d; return d+1
sf: procedure; parse arg x; != 1; do #=2 to x; != ! * !(#); end; return !
hf: procedure; parse arg x; != 1; do #=2 to x; != ! * #**#; end; return !
rf: procedure; parse arg x; do #=0 until f>=x; f=!(#); end; return rfr()
rfr: if x==f then return #; ?= 'undefined'; return ?
hdr: parse arg ?,,$; say 'the first ten '?"factorials:"; return
tell: say substr($, 2); say; $=; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
the first 10 superfactorials:
1 1 2 12 288 34560 24883200 125411328000 5056584744960000 1834933472251084800000


numeric digits 35
the first 10 hyperfactorials:
1 1 4 108 27648 86400000 4031078400000 3319766398771200000 55696437941726556979200000 21577941222941856209168026828800000


line = "superfactorials 0-9: "
the first 10 alternating factorials:
do n = 0 to 9
0 1 1 5 19 101 619 4421 35899 326981
line = line superfactorial(n)
end
say line


line = "hyperfactorials 0-9: "
the first 5 exponential factorials:
1 1 2 9 262144
do n = 0 to 9
line = line hyperfactorial(n)
end
say line


line = "alternating factorials 0-9:"
the number of decimal digits in the exponential factorial of 5 is:
do n = 0 to 9
183,231
line = line alternatingfactorial(n)
end
say line


line = "exponential factorials 0-4:"
the inverse factorial of 1 is: 0
do n = 0 to 4
the inverse factorial of 2 is: 2
line = line exponentialfactorial(n)
the inverse factorial of 6 is: 3
end
the inverse factorial of 24 is: 4
say line
the inverse factorial of 120 is: 5

the inverse factorial of 720 is: 6
the inverse factorial of 5040 is: 7
say "exponential factorial 5: ",
length(format(exponentialfactorial(5), , , 0)) "digits"
the inverse factorial of 40320 is: 8

the inverse factorial of 362880 is: 9
the inverse factorial of 3628800 is: 10
line = "inverse factorials: "
numbers = "1 2 6 24 120 720 5040 40320 362880 3628800 119"
the inverse factorial of 119 is: undefined
do i = 1 to words(numbers)
line = line inversefactorial(word(numbers,i))
end
say line

return


superfactorial: procedure
parse arg n
sf = 1
f = 1
do k = 1 to n
f = f * k
sf = sf * f
end
return sf

hyperfactorial: procedure
parse arg n
hf = 1
do k = 1 to n
hf = hf * k ** k
end
return hf

alternatingfactorial: procedure
parse arg n
af = 0
f = 1
do i = 1 to n
f = f * i
af = af + (-1) ** (n - i) * f
end
return af

exponentialfactorial: procedure
parse arg n
ef = 1
do i = 1 to n
ef = i ** ef
end
return ef

inversefactorial: procedure
parse arg f
n = 1
do i = 2 while n < f
n = n * i
end
if n = f then
if i > 2 then
return i - 1
else
return 0
else
return "undefined"
</syntaxhighlight>
{{out}}
<pre>
superfactorials 0-9: 1 1 2 12 288 34560 24883200 125411328000 5056584744960000 1834933472251084800000
hyperfactorials 0-9: 1 1 4 108 27648 86400000 4031078400000 3319766398771200000 55696437941726556979200000 21577941222941856209168026828800000
alternating factorials 0-9: 0 1 1 5 19 101 619 4421 35899 326981
exponential factorials 0-4: 1 1 2 9 262144
exponential factorial 5: 183231 digits
inverse factorials: 0 2 3 4 5 6 7 8 9 10 undefined
</pre>
</pre>