pi_challenge.py

Created by schraf

Created on April 12, 2020

786 Bytes

Explications en vidéo

Le programme ne fonctionne pas dans l’émulateur mais devrait fonctionner sur votre machine.


D = 102
T = 5  # on veut 5*102 = 500 decimales

def unSur(d):
  r =[0] * D 
  n = 1
  for i in range(D):
    for j in range(T):
     q = n // d
     r[i] *= 10
     r[i] += q
     n = (n - q * d) * 10
  return r

def add(a,b):
  for i in range(D):
    v = a[i] + b[i]
    if v > 10**T and i > 0: a[i-1] += 1
    a[i] = v % 10**T
  return a

def atan(x, n):
  r = [0] * D
  c = x * x
  p = 1
  for i in range(n):
    r = add(r,unSur(p * x))
    p += 2
    if x < 0: r[D - 1] += 1
    x *= -c
  return r

def aff(r):
  for i in range(D):
    print(("0"*T + str(r[i]))[-T:],end="")

def pi():
  v = atan(5, 360)
  w = atan(-239, 106)
  r = add(v,v) # 2atan(1/5)
  r = add(r,r) # 4atan(1/5)
  r = add(r,w) # 4atan(1/5)-atan(1/239)
  r = add(r,r)
  r = add(r,r)
  aff(r)

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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.