Post Reply 
Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
11-14-2019, 02:01 AM
Post: #23
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(11-14-2019 12:04 AM)Albert Chan Wrote:  
(11-11-2019 06:14 PM)Gerson W. Barbosa Wrote:  n = 1663 will be enough for 1000 digits:

It may be better to make n even, avoiding 1 square root:

\(\Large \frac{\pi}{\log 2}\normalsize ≈ AGM(2n, \frac{2n}{2^{n-2}}) = AGM(n + \frac{n}{2^{n-2}}, \frac{2n}{2^{n/2-1}}) \)

With n=1662, loop count down to 17.

Code:
OPTION ARITHMETIC DECIMAL_HIGH
LET n = 1662
LET nd = 1000
PRINT "Log(2) = "
LET t = TIME 
LET c = 2^(n/2-1)
LET a = n + n/(c*c)
LET b = (n+n)/c
FOR i = 1 TO 17
   LET c = a
   LET a = (a + b)/2
   LET b = SQR(b*c)
NEXT i
LET c = (a + b)/2
LET a = c - (a-b)^2/(16*c)
LET g = PI/a
LET r = TIME - t
LET r$ = STR$(g)                           
PRINT
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
      PRINT "  ";
   END IF
NEXT i
IF MOD (i - 2,50) <> 0  OR nd = 0 THEN PRINT
PRINT 
PRINT "Runtime: ";
PRINT  USING "0.##": r;
PRINT " seconds"
END

I`ve just tested your optmization. For conciseness, here are the last lines of the ouput:

...
7891923723 1467232172 0534016492 5687274778 2344535347

Runtime: 0.03 seconds


The running times vary, depending on whatever other tasks the OS is doing. The ones I've posted are the minimum values I've observed for each example.

This appears to be the record here for one thousand correct digits.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others] - Gerson W. Barbosa - 11-14-2019 02:01 AM



User(s) browsing this thread: 1 Guest(s)