Post Reply 
Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
10-12-2019, 11:16 PM
Post: #10
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
(10-12-2019 10:22 PM)toml_12953 Wrote:  Here's a Python program modeled after the Decimal BASIC version but it doesn't have extended precision. I'm new to Python and don't know how to get the most out of it.

Code:
def log_2():
    import math,time
    nd=input("Number of decimal places: ")
    nd=int(nd)
    t = time.time()
    k = math.ceil(nd/math.log(8))
    n = k*k
    m = 2*k - 1
    c = 8
    d0 = c*(k - 2) + 10
    d1 = 2*k*(2*k - 1)
    d2 = 4*k + 1
    s1 = 0
    s2 = 0
    for i in range(1,k+1):
        s1 = s1 + 1/d1
        d1 = d1 - d0
        d0 = d0 - c
        s2 = n/(s2 + d2)
        n = n - m
        m = m - 2
    s2 = 1/(s2 + d2)
    r = time.time() - t
    rs = str(s1 + s2)
    print()
    print(" ",end="")
    print(rs[0:2],end="")
    for i in range(2,nd+2):
        print(rs[i:i+1],end="")
        if (i - 1) % 10 == 0:
            print(" ",end="")
        if (i - 1) % 50 == 0:
            print()
            print(" ",end="")
    if (i - 2) % 50 != 0 or nd == 0:
        print()
    print()
    print("Runtime: ",end="")
    print('%4.2f' %r ,end="")
    print(" seconds")

Decimal BASIC is easy to use, but it’s limited to only 1000 digits.
I’ve noticed this algorithm produces about 2.09 digits per iteration, hence the use of ln(8) in the estimation of the required number of iterations for a given number of 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 - 10-12-2019 11:16 PM



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