apmep.py

Created by schraf

Created on March 31, 2026

1.5 KB

Enoncé du problème sur le forum Silicium


PREC  = 60
SCALE = 10 ** PREC

def ln2():
    result = 0
    power = SCALE // 3
    base  = SCALE // 9
    k = 0
    while True:
        term = power // (2 * k + 1)
        if term == 0: break
        result += term
        power = (power * SCALE) // (9 * SCALE // SCALE)
        power = (power * SCALE) // (9 * SCALE)
        k += 1
    return 2 * result

def ln2_direct():
    result = 0
    num   = SCALE // 3
    denom = 9
    k = 0
    while True:
        term = num // (2 * k + 1)
        if term == 0: break
        result += term
        num = num // denom
        k += 1
    return 2 * result

def f_series(x):
    result = 0
    xk = (x * x) // SCALE   # x^2
    k  = 2
    while True:
        term = xk // k
        if term == 0: break
        if k % 2 == 0: result += term
        else: result -= term
        xk = (xk * x) // SCALE
        k  += 1
    return result

def int_to_digits(n, nb):
    digits = []
    for _ in range(nb):
        n, r = divmod(n, 10)
        digits.append(r)
    digits.reverse()
    return digits

def affiche(n, val):
    if val < 0: s, val = "-", -val
    else: s = ""
    entier = val // SCALE
    frac   = val % SCALE
    e_digits = int_to_digits(entier, 1)
    f_digits = int_to_digits(frac, PREC)
    ligne = "u(" + str(n) + ") = " + s
    for d in e_digits: ligne += str(d)
    ligne += "."
    for d in f_digits: ligne += str(d)
    print(ligne)

u = SCALE
affiche(0, u)
u = SCALE - ln2_direct()
affiche(1, u)
for n in range(1, 8):
    u = f_series(u)
    affiche(n + 1, u)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.