champernowne.py

Created by nicolas-patrois

Created on February 16, 2020

272 Bytes

Calcule la n-ième décimale du nombre de Champernowne (0,123456789101112131415…) avec deux fonctions. brutale ne permet pas d’aller trop loin, maline est plus rapide et plus économe.


def champer(n):
  return ((9*n-1)*10**n+1)//9

def brutale(n):
  s=""
  i=1
  while len(s)<n:
    s+=str(i)
    i+=1
  return s[n-1]

def maline(n):
  i=0
  while champer(i)<n:
    i+=1
  i-=1
  q,r=divmod(n-champer(i)-1,i+1)
  return str(q+10**(i))[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 cookies policy.