top of page

Project Euler Problem 40 - Champernowne's Constant

  • Brodie Mooy
  • Jul 23
  • 2 min read

Project Euler Problem 40 revolves around Champernowne's constant, which is formed by writing the positive integers one after another:


0.1234567891011121314151617...


The task is to find the digits at positions:


d₁


d₁₀


d₁₀₀


d₁₀₀₀


d₁₀₀₀₀


d₁₀₀₀₀₀


d₁₀₀₀₀₀₀


and multiply them together.



My Approach



Instead of working out the position of each digit mathematically, I decided to build the sequence as a string and extract the digits I needed.


For each required position, I generated the concatenated sequence of numbers, sliced it to the desired length, and took the last character. This isn't the most efficient method because it rebuilds the string multiple times, but it's straightforward and easy to understand.


One thing to note is that I didn't generate enough digits to reach the one-millionth position. Instead, after calculating the product of the first six required digits, I printed every possible multiple (0–9) because the final digit at the millionth position must be between 0 and 9.



Exemplar Python Code:



d1 = 1


string2 = ""


for i in range(1,12):


    string2 += str(i)


d10x = string2[:10]



d10 = d10x[-1]


print(d10)



string3 = ""


for i in range(1,100):


    string3 += str(i)



d100x = string3[:100]


d100 = d100x[-1]


print(d100)



string4 = ""


for i in range(1,1000):


    string4 += str(i)



d1000x = string4[:1000]


d1000 = d1000x[-1]


print(d1000)




string5 = ""


for i in range(1,10000):


    string5 += str(i)



d10000x = string5[:10000]


d10000 = d10000x[-1]


print(d10000)




string6 = ""


for i in range(1,100000):


    string6 += str(i)



d100000x = string6[:100000]


d100000 = d100000x[-1]


print(d100000)




x = (int(d1) int(d10) int(d100) int(d1000) int(d10000) * int(d100000))



print("Possible solutions are: ")


print(x * 0)


print(x * 1)


print(x * 2)


print(x * 3)


print(x * 4)


print(x * 5)


print(x * 6)


print(x * 7)


print(x * 8)


print(x * 9)


The Solution


Running this program gives the first six required digits:


d₁ = 1


d₁₀ = 1


d₁₀₀ = 5


d₁₀₀₀ = 3


d₁₀₀₀₀ = 7


d₁₀₀₀₀₀ = 2


Their product is:


1 × 1 × 5 × 3 × 7 × 2 = 210


Since my program did not yet calculate the digit at position 1,000,000, it printed all possible answers:


Possible solutions are:


0


210


420


630


840


1050


1260


1470


1680


1890



The correct value for d₁₀₀₀₀₀₀ is 1, making the final answer:


210



Reflections



Although this isn't the most efficient solution, it was a useful way to solve the problem with simple Python techniques such as loops, string concatenation, slicing, and integer conversion.



If I were to revisit this solution, I would generate the sequence only once until it exceeded one million digits, then retrieve each required digit directly using indexing. That would eliminate the repeated code and make the program significantly faster and cleaner.



Even so, this solution was a good exercise in breaking a mathematical problem into manageable programming steps, and it reinforced the importance of writing a working solution before focusing on optimization.

 
 
 

Comments


bottom of page