-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem=40.py
More file actions
28 lines (22 loc) · 825 Bytes
/
problem=40.py
File metadata and controls
28 lines (22 loc) · 825 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Champernowne's Constant - https://projecteuler.net/problem=40
def champernowne_digit(n):
"""Return the nth digit of Champernowne's constant."""
digit_length = 1
count = 9
start = 1
# Find the range in which the nth digit lies
while n > digit_length * count:
n -= digit_length * count
digit_length += 1
count *= 10
start *= 10
# Find the actual number that contains the nth digit
number = start + (n - 1) // digit_length
digit_index = (n - 1) % digit_length
return int(str(number)[digit_index])
if __name__ == "__main__":
positions = [1, 10, 100, 1000, 10000, 100000, 1000000]
product = 1
for pos in positions:
product *= champernowne_digit(pos)
print(f"The product of the digits at the specified positions is: {product}")