|
(50G) full digit nth root of any real number/huge alg. expression, |n| an integer>1 +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (https://www.hpmuseum.org/forum/forum-10.html) +--- Forum: General Software Library (https://www.hpmuseum.org/forum/forum-13.html) +--- Thread: (50G) full digit nth root of any real number/huge alg. expression, |n| an integer>1 (/thread-23796.html) |
(50G) full digit nth root of any real number/huge alg. expression, |n| an integer>1 - Gil - 2025-07-13 To calculate full digit nth root (n beeing an integer, with abs(n)> 1, or a real number, with abs(n) >1, whose fraction part = 0) of any real number or (huge) algebraic expression containing +-*/ operations (but no functions like LN, SIN, EXP, etc.), launch the following program rootN placed in the same directory as the other program manyD. rootN "\\<< \"1 Arg: Version3 {A n d} or {A n } .A: real/integer/alg.exp .nth Root: |n|\\>=2 & FP(n)=0 .d: 22\\<=digits\\<=352 .If {A n } \\->d=44 dig .X1=X0 - f(X0)/f'(XO) \" DROP DUP SIZE 2 == { 44 + } IFT OBJ\\-> DROP UNROT OVER TYPE UNROT DUP2 DUP 0 == IF THEN 1 + END 1 CF -3 CF -105 SF SWAP \\->NUM DUP 0 \\>= 1 -1 IFTE R\\->I DUP 8 ROLLD * SWAP INV ^ \\->NUM -105 CF DUP FP 0 == { R\\->I } IFT 0 0 0 0 0 0 0 \\-> s d t A n X l ii A2 ls ds A0 inp \\<< A 'A0' STO s 'A' STO* IF n ABS 2 < n FP 0 \\=/ OR THEN { A0 n d } \"You must have: |n|\\>=2 & FP(n)=0\" DOERR END A manyD PICK3 EVAL A0 SWAP DUP2 DTAG SWAP ABS SAME IF THEN DROP \\->STR ELSE DUP TYPE 't' STO s * EVAL \\->STR SWAP \\->STR \" =\" + SWAP + END t 9 == IF THEN \" =\" + A0 \\->NUM + END \" ^(1/\" + n R\\->I DUP 'n' STO + \"), \" + d 352 MIN 22 MAX 11 / LN 2 LN / \\->NUM CEIL 'd' STO 11 2 d ^ * R\\->I + \"d\" + 'inp' STO \"X\" 1 d START \"X\" \"((n-1)/n*X+(A)/n/X^(n-1))\" SREPL DROP NEXT \"X\" X \\->STR SREPL DROP \"A\" A \\->STR SREPL DROP \"'\" \"\" SREPL DROP \"n\" n \\->STR SREPL DROP \"'\" + \"'\" SWAP + OBJ\\-> manyD DEPTH DUP 'ds' STO \\->LIST 'l' STO 1 ds FOR i l i GET DUP TYPE 12 == IF THEN OBJ\\-> SWAP DROP \"Input\" SAME IF THEN i 'ii' STO ds 'i' STO END ELSE DROP END NEXT l OBJ\\-> ii 1 - - DROPN inp l REVLIST DUP 2 GET SWAP 1 GET DUP2 == IF THEN SWAP DROP s * \"=\" \\->TAG ELSE \\->STR 1 11 2 d ^ * SUB OBJ\\-> s * SWAP s * SWAP DUP2 SWAP DTAG \\->STR \".\" \"\" SREPL DROP DUP SIZE 1 + PICK3 SIZE START \"0\" + NEXT OBJ\\-> SAME IF THEN DROP \"=\" \\->TAG ELSE SWAP \"~\" \\->TAG SWAP END END \\>> \\>>" " manyD \\<< \"1 Arg Version8 \\|>ex '7+3.!*(2*10.^600. )/(4-3.27E-15)*4.^-7.' or '73179/17^2/13' or .275 or 3.275 \\|>mix 4 operat. + - * / & any real/integ/frac \\|>& x^y: x real/integer/fract y integer or FP(y)=0 \\-> '(2./3.)^3.' : ok '(2./3.)^-3.': ok '3^(1/2)': not ok \\|>& x!: x\\>=0 & x integer or FP(x)=0 \\|>xE-499xE0 xE499 OK: |xEy| <1E500, x real, -499\\<=y_integer\\<=499 \\|>for |#| \\>= 1E500, say 2E600, write (2.*10.^600.) or (2*10^600) always with () \\|> If intermed.|result| \\<=1E-500 \\->impossible: 7 * (2^600) \\-> ok 7 / (2^600) \\-> error 1.E-450*1.E-99\\-> err \\|>in '': no var-name & no \\v/ SIN LN EXP etc. \\|>local var n (min di- gits) by default=360. 6 lines below, you can change it. \" DROP STD RAD -3 CF -20 SF -21 CF -105 CF DTAG DUP DUP TYPE 0 0 0 0 0 0 { } { \"E-\" \"E\" } 360 \"default: 360\" DROP \\-> x num t ls ds p p1 p2 o lx lE n \\<< \\<< DUP2 \\->STR SWAP DTAG \\->STR \"E\" POS 1 - OVER SIZE DUP2 \"\" UNROT START \"0\" + NEXT 4 ROLLD SUB \\=/ IF THEN SWAP DTAG \"~\" \\->TAG SWAP END DEPTH DUP 'ds' STO \\->LIST REVLIST 'ls' STO 2 ds FOR i ls i GET DUP TYPE 12 == IF THEN OBJ\\-> \"num\" == IF THEN \"\\->wrong!\" \\->TAG i SWAP ls UNROT PUT ds 'i' STO ELSE DROP END ELSE DROP END NEXT REVLIST OBJ\\-> DROP \\>> \\-> msg \\<< x \"Input\" \\->TAG t 0 \\=/ t 28 \\=/ AND IF THEN DUP IFERR \\->NUM THEN \"Try, instead input 'a', '1/(a)'. Res='1/Res'\" DOERR END DUP DUP XPON ABS R\\->I 'n' STO+ 'num' STO \"num\" \\->TAG END x \\->STR \".!\" \"!\" SREPL DROP \"'\" \"\" SREPL DROP 'x' STO 1 2 FOR i WHILE x lE i GET POS DUP 'p' STO 0 \\=/ REPEAT 0 'p1' STO 0 'p2' STO p 1 - 1 FOR i x i i SUB 'o' STO IF o \"+\" SAME o \"-\" SAME OR o \"*\" SAME OR o \"/\" SAME OR o \"^\" SAME OR o \"(\" SAME OR THEN i 'p1' STO 0 'i' STO END -1 STEP x 1 p1 SUB \"(\" + x p1 1 + p 1 - SUB + i 1 == \"/\" \"*\" IFTE + p i NEG 4 + + x SIZE FOR i x i i SUB 'o' STO IF o \"+\" SAME o \"-\" SAME OR o \"*\" SAME OR o \"/\" SAME OR o \"^\" SAME OR o \")\" SAME OR THEN i 'p2' STO 0 'i' STO END NEXT p2 0 == IF THEN x SIZE 1 + 'p2' STO END x p i NEG 3 + + p2 1 - SUB OBJ\\-> ALOG + \")\" + x p2 x SIZE SUB + 'x' STO END NEXT WHILE x \".\" POS DUP 'p' STO 0 \\=/ REPEAT 0 'p1' STO 0 'p2' STO 0 'o' STO p 1 - 1 FOR i x i i SUB 'o' STO IF o \"+\" SAME o \"-\" SAME OR o \"*\" SAME OR o \"/\" SAME OR o \"^\" SAME OR o \"(\" SAME OR THEN i 'p1' STO 0 'i' STO END -1 STEP IF p1 0 \\=/ THEN x 1 p1 SUB 'lx' STO+ END \"(\" x p1 1 + p 1 - SUB + 'lx' STO+ p 1 + x SIZE FOR i x i i SUB 'o' STO IF o \"+\" SAME o \"-\" SAME OR o \"*\" SAME OR o \"/\" SAME OR o \"^\" SAME OR o \")\" SAME OR THEN i 'p2' STO 0 'i' STO END NEXT IF p2 0 \\=/ THEN x p 1 + p2 1 - SUB \"/\" + p2 p - 1 - ALOG R\\->I + \")\" + 'lx' STO+ x p2 DUP SUB 'lx' STO+ x p2 1 + x SIZE SUB 'x' STO ELSE x p 1 + x SIZE SUB \"/\" + x SIZE p - ALOG R\\->I + \")\" + 'lx' STO+ \"\" 'x' STO END END x 'lx' STO+ lx DUP SIZE 0 \\=/ IF THEN REVLIST 'lx' STO \"\" 1 lx SIZE FOR i lx i GET + NEXT \"'\" + \"'\" SWAP + OBJ\\-> DUP2 SAME { DROP } IFT DUP EVAL DUP2 SAME { DROP } IFT EVAL DUP TYPE 9 == IF THEN DUP OBJ\\-> DROP2 ABS SWAP ABS < IF THEN DUP PROPFRAC END t 0 \\=/ IF THEN DUP n ALOG R\\->I * PROPFRAC PROPFRAC DUP TYPE 9 == IF THEN OBJ\\-> 3 DROPN END DUP \\->STR DUP DUP SIZE DUP n 1 + - R\\->I ROT 1 1 SUB \".\" + 4 ROLL 4 ROLL 2 SWAP SUB + OBJ\\-> \"E\" + OVER + SWAP ABS 500 < IF THEN OBJ\\-> END SWAP DUP2 \\->STR SWAP DUP TYPE 2 \\=/ { \\->STR } IFT DUP \"E\" POS DUP 0 \\=/ IF THEN 1 - 1 SWAP SUB ELSE DROP END \".\" \"\" SREPL DROP SIZE 1 + OVER SIZE DUP2 \"\" UNROT START \"0\" + NEXT 4 ROLLD SUB SAME NOT IF THEN SWAP \"~\" \\->TAG SWAP END OVER DUP DTAG TYPE 2 \\=/ IF THEN num - PICK3 / ABS .0000000001 > IF THEN msg EVAL END ELSE DROP msg EVAL END END ELSE DUP DUP \\->STR DUP DUP 1 1 SUB \".\" + UNROT SIZE DUP 4 ROLLD 2 SWAP SUB + OBJ\\-> SWAP 1 - R\\->I DUP UNROT \"E\" SWAP + + SWAP ABS 499 \\<= { OBJ\\-> } IFT SWAP OVER DUP DTAG TYPE 2 \\=/ IF THEN num - PICK3 / ABS .0000000001 > IF THEN msg EVAL END ELSE TYPE 2 == IF THEN msg EVAL ELSE DUP ABS LOG num ABS LOG -105 SF \\=/ { msg EVAL } IFT END END END END \\>> \\>> \\>> Example input Cubic root of 29.1/1.3 with 60 digits accuracy {'29.1/1.3' 3 60} rootN Example result "'29.1/1.3' ='291/13' =22.3846153846 ^(1/3), 88d" :~: 2.818273988 2818273987996238408541866938109913983599721872486686698084448911457926268410135830391437 Note The number relative to the digits accuracy output is fixed between 22 and 352 and is always a multiple of 22. If you don't specify in the input list the third element, ie the requested digit-precision, then the accuracy will be calculated with 44 digits. |