Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
|
11-11-2019, 06:14 PM
Post: #17
|
|||
|
|||
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(11-08-2019 03:18 PM)Albert Chan Wrote: Update: More precise calculations shows that n=20 over-estimated log(2) by 2e-12 n = 1663 will be enough for 1000 digits: -------------------------------------------------------- OPTION ARITHMETIC DECIMAL_HIGH LET n = 1663 LET nd = 1000 PRINT "Log(2) = " LET t = TIME LET p = 2^(n - 3) LET d = PI*p LET a = 1 LET b = p + p FOR i = 1 TO 20 LET c = a LET a = (a + b)/2 LET b = SQR(b*c) NEXT i LET g = d/(a*n) LET r = TIME - t LET r$ = STR$(g) PRINT "0"; PRINT r$(0:1); FOR i = 2 TO nd + 1 PRINT r$(i:i); IF MOD((i - 1),10) = 0 THEN PRINT " "; IF MOD((i - 1),50) = 0 THEN PRINT " "; END IF NEXT i IF MOD (i - 2,50) <> 0 OR nd = 0 THEN PRINT PRINT "Runtime: "; PRINT USING "0.##": r; PRINT " seconds" END -------------------------------------------------------- Log(2) = 0.6931471805 5994530941 7232121458 1765680755 0013436025 5254120680 0094933936 2196969471 5605863326 9964186875 4200148102 0570685733 6855202357 5813055703 2670751635 0759619307 2757082837 1435190307 0386238916 7347112335 0115364497 9552391204 7517268157 4932065155 5247341395 2588295045 3007095326 3666426541 0423915781 4952043740 4303855008 0194417064 1671518644 7128399681 7178454695 7026271631 0645461502 5720740248 1637773389 6385506952 6066834113 7273873722 9289564935 4702576265 2098859693 2019650585 5476470330 6793654432 5476327449 5125040606 9438147104 6899465062 2016772042 4524529612 6879465461 9316517468 1392672504 1038025462 5965686914 4192871608 2938031727 1436778265 4877566485 0856740776 4845146443 9940461422 6031930967 3540257444 6070308096 0850474866 3852313818 1676751438 6674766478 9088143714 1985494231 5199735488 0375165861 2753529166 1000710535 5824987941 4729509293 1138971559 9820565439 2871700072 1808576102 5236889213 2449713893 2037843935 3088774825 9701715591 0708823683 6275898425 8918535302 4363421436 7061189236 7891923723 1467232172 0534016492 5687274778 2344535347 Runtime: 0.08 seconds -------------------------------------------------------- That's one fourth of the time required by my original program based on series combined with a continued fraction. Only 20 iterations here compared to 481 there, but AGM requires the evaluation of one square root per iteration. I'll post later another solution based on a previously suggested algorithm which does it in 0.04 seconds. It would be better to compare those running times on a compiled language, though. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)