Last active
April 24, 2021 20:48
-
-
Save betaprojects/b82ed6355ffef234c2fc05835f77a546 to your computer and use it in GitHub Desktop.
Project Euler & HackerRank problem 40 solution: Champernowne’s constant - solved using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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