Skip to content

Instantly share code, notes, and snippets.

@betaprojects
Last active April 24, 2021 20:48
Show Gist options
  • Save betaprojects/b82ed6355ffef234c2fc05835f77a546 to your computer and use it in GitHub Desktop.
Save betaprojects/b82ed6355ffef234c2fc05835f77a546 to your computer and use it in GitHub Desktop.
Project Euler & HackerRank problem 40 solution: Champernowne’s constant - solved using Python
q = [9*(x+1) * pow(10, x) for x in range(20)]
def d(n): # find the digit at position n in Champernowne's constant
i = 0
while n>q[i]: n-= q[i]; i+= 1
L, d = divmod((n-1), i+1)
return int(str(pow(10, i)+L)[d])
n = input("Enter some indexes, separated by a space? ")
m = 1
for ci in map(int, n.split()):
m*= d(ci)
print ("product =", m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment