(42S) Big Factorial
|
12-15-2024, 11:39 PM
Post: #1
|
|||
|
|||
(42S) Big Factorial
This program allows to calculate all digits of \(n!\):
Code: 00 { 61-Byte Prgm } Question Is there a better way to shift the elements of an array to the left? Initialisation We create an array 1 of length 10 and use 10 digits in each element. 1 ENTER 10 NEWMAT EDIT ← 1 EXIT STO "1" E 10 STO 00 Example 69 XEQ "BIG!" [ 1×10 Matrix ] 171'122'452 4'281'413'113 7'246'833'888 1'272'839'092 2'705'448'935 2'036'939'364 8'040'923'257 2'797'541'406 4'742'400'000 0 Compare this to: 69! = 171122452'4281413113'7246833888'1272839092'2705448935'2036939364'8040923257'2797541406'4742400000'0000000000 Here we use 10 digits for each element of the array. For higher values of \(n\) this needs to be reduced accordingly. The result of multiplying by \(n\) must fit into the 12 digits provided by the HP-42S. Using Free42 with its 34 decimal digits allows to calculate \(1000!\) without further adjustments. You just have to create the array 1 as a [ 1×257 Matrix ]. |
|||
12-16-2024, 03:40 PM
Post: #2
|
|||
|
|||
RE: (42S) Big Factorial
A quick hack to see if it could be done:
All in the stack, with an automatically growing matrix. I add a zero to the end (right) of the carry matrix with GROW J+, positioned at the end. I shift the carry matrix left with a DELR if the leftmost carry is zero, else I enlarge the value matrix with an INSR, adding a zero to the left. The 1E10 can be calculated if needed, to be as large as possible without overflowing. 00 { 70-Byte Prgm } 01▸LBL "BIG!" 02 1 03 ENTER 04 NEWMAT 05 1 06 + 07 1ᴇ10 08 X<>Y 09▸LBL 02 10 RCL ST Z 11 × 12 RCL ST Y 13 ÷ 14 ENTER 15 FP 16 STO- ST Y 17 RCL× ST Z 18 X<>Y 19 EDIT 20 J- 21 GROW 22 J+ 23 I+ 24 X=0? 25 GTO 00 26 EXITALL 27 X<>Y 28 EDIT 29 INSR 30 GTO 01 31▸LBL 00 32 DELR 33▸LBL 01 34 EXITALL 35 + 36 DSE ST Z 37 GTO 02 38 END Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
12-16-2024, 05:38 PM
Post: #3
|
|||
|
|||
RE: (42S) Big Factorial
Very nice.
With this we can calculate \(508!\). At least on the Free42. The next challenge is to print it as a tree. I'm not sure if that's possible though. Maybe we need to use a smaller one, e.g. \(284!\). Or we print the trailing zeros as trunk? I must admit that I was inspired by this recent post: |
|||
12-16-2024, 08:52 PM
Post: #4
|
|||
|
|||
RE: (42S) Big Factorial
Well, \(105!\) might work:
1 081 39675 8240290 900504101 30580032964 9720646107774 902579144176636 57322653190990515 3326984536526808240 339776398934872029657 99387290781343681609728 000 000 000 000 000 000 0000000 We only have to do it based on these numbers: 108'139'675 8'240'290'900 5'041'013'058 32'964'972 646'107'774 9'025'791'441 7'663'657'322 6'531'909'905 1'533'269'845 3'652'680'824 339'776'398 9'348'720'296 5'799'387'290 7'813'436'816 972'800'000 0 0 |
|||
12-16-2024, 10:54 PM
Post: #5
|
|||
|
|||
RE: (42S) Big Factorial
It was a bit tedious to do it manually, but at least I have an idea how this could be done with a program.
But I cheated a bit printing the trunk. |
|||
12-17-2024, 10:43 PM
(This post was last modified: 12-17-2024 11:07 PM by Thomas Klemm.)
Post: #6
|
|||
|
|||
RE: (42S) Big Factorial
I would have liked to use Werner's excellent program with:
Code: 07 1ᴇ1 Therefore I use: Code: 07 1ᴇ2 But now we have to split 2-digit numbers and align them with: Code: 00 { 72-Byte Prgm } The variable "." now contains all the digits in a \(n \times 1\) matrix. The value 48 was added because that is the value for 0 when using XTOA. Once we have that, we extract groups of digits for each row and combine them with a shrinking list of spaces in the variable " ". The input for this program is the number of lines to be printed: Code: 00 { 66-Byte Prgm } Example 105 XEQ "BIG!" [ 85×1 Matrix ] XEQ "ALIGN" 12 XEQ "TREE" The result is similar to the picture already posted. However, the trunk built with zeros is missing. I'm leaving that as an exercise. |
|||
12-17-2024, 11:03 PM
(This post was last modified: 12-17-2024 11:08 PM by Thomas Klemm.)
Post: #7
|
|||
|
|||
(42S) Tree Factorial
This creates another example of a tree factorial:
81 XEQ "BIG!" [ 61×1 Matrix ] XEQ "ALIGN" 11 XEQ "TREE" |
|||
12-19-2024, 06:56 AM
Post: #8
|
|||
|
|||
RE: (42S) Big Factorial | |||
12-19-2024, 07:38 PM
Post: #9
|
|||
|
|||
RE: (42S) Big Factorial
(12-16-2024 05:38 PM)Thomas Klemm Wrote: Very nice. Hi Thomas and Werner, I must admit that the calculation of factorials must have inspired many of us and often encouraged us to surpass ourselves. Always further and always faster if possible! This led me to learn how to program my SHARP PC-1251 in SC61860 assembler to recover all the digits of increasingly larger factorials!! Thank you for all this research and the article shared. Merry Christmas! Keep you safe Laurent |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)