Post Reply 
Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
10-12-2019, 10:22 PM (This post was last modified: 11-12-2019 08:32 PM by toml_12953.)
Post: #9
RE: Natural logarithm of 2 [HP-15C/HP-42S/Free42 & others]
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=int(input("Number of decimal places: "))
    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")

Tom L
Cui bono?
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] - toml_12953 - 10-12-2019 10:22 PM



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