Project Euler Problem #40:

Problem #40 involves an obscure constant in mathematics known as the Champernowne constant. This constant is relevant because it is one of the few numbers that has been proven to be transcendental. The problem reads:

Project Euler Problem 40: Champernowne's constant
An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021...

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fractional part, find the value of the following expression.

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

If you expected an elegant method of finding the nth digit of Champernowne’s constant, I’m sorry, but you’re probably going to be disappointed.

Solution #1: Brute Force Approach

We count digits of the first x numbers until we get the number x with the nth digit in Champernowne’s constant. We do this for each of the digits in the product and multiply the results. Here is an implementation of this method in Python 2.7:

 '''
 Author: Walker Kroubalkian
 Brute Force Approach to Project Euler Problem #40
 '''
 
 import time
 from math import ceil
 
 def calculateChampernowneDigit(n):
     c = 1
     total = 0
     while(n-total>c*9*(10**(c-1))):
         total+=c*9*(10**(c-1))
         c+=1
     n-=total
     numbers = int(ceil(1.0*n/(1.0*c)))
     theNumber = 10**(c-1) + numbers-1
     a = str(theNumber)
     return int(a[((n-1)%c)])
 
 def projectEulerProblemForty(myList):
     total = 1
     for x in myList:
         total*=calculateChampernowneDigit(x)
     return total
 
 start = time.time()
 print projectEulerProblemForty([1,10,100,1000,10000,100000,1000000])
 print ("--- %s seconds ---" % (time.time()-start))
 
 '''
 Prints
 
 210
 --- 3.88622283936e-05 seconds ---
 
 for input of myList = [1,10,100,1000,10000,100000,1000000]
 ''' 

As shown above, there are many questions in mathematics where inelegant solutions are acceptable.

Thanks for reading!

Published by Walker Kroubalkian

My name is Walker Kroubalkian. I really enjoy math, computer science, and hiking.

Leave a comment

Design a site like this with WordPress.com
Get started