Champernowne's constant
An irrational decimal fraction is created by concatenating the positive integers:
If dn represents the nth digit of the fractional part, find the value of the following expression.
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
This problem requires a small homework to set the upper limit for the for loop. This is general mathematics.
We have 9, 1 digit numbers
90, 2 digit numbers
900, 3 digit numbers
9000, 4 digit numbers
90000, 5 digit numbers
900000, 6 digit numbers
Which will total to 5888889 digits. But we only need 1000000, so that
9 x 1 digit
90 x 2 digit
900 x 3 digit
9000 x 4 digit
90000 x 5 digit
n x 6 digit
= 9*1 + 90*2 + 900*3+ 9000*4 + 90000*5 + n*6 = 1000000
So the value of n is approximately 85186 that means that we will have to do iterations until 185186.
The same thing has been used in the program directly.
Remember that the strings are indexed from 0 on wards.
As always you can download the source code from Github Gist pep40.py
This solution also satisfies PEP8 standard.
As always if you have any doubt or didn't understand anything then you can comment in the comment box below, I will be glad to help you.
Please do comment if you have found any typo or have any better program or have a different program or have any suggestion. I will be glad to view each of them.
You can also contact me.
Thank you, have a nice day😃.
We have 9, 1 digit numbers
90, 2 digit numbers
900, 3 digit numbers
9000, 4 digit numbers
90000, 5 digit numbers
900000, 6 digit numbers
Which will total to 5888889 digits. But we only need 1000000, so that
9 x 1 digit
90 x 2 digit
900 x 3 digit
9000 x 4 digit
90000 x 5 digit
n x 6 digit
= 9*1 + 90*2 + 900*3+ 9000*4 + 90000*5 + n*6 = 1000000
So the value of n is approximately 85186 that means that we will have to do iterations until 185186.
The same thing has been used in the program directly.
Program
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
# http://radiusofcircle.blogspot.com | |
# time module for calculating execution time | |
import time | |
# time at the start of program execution | |
start = time.time() | |
# variable to store the values | |
a = '' | |
# for loop | |
for i in xrange(1, 185186): | |
a += str(i) | |
# d1 | |
d1 = int(a[0]) | |
# d10 | |
d10 = int(a[9]) | |
# d100 | |
d100 = int(a[99]) | |
# d1000 | |
d1000 = int(a[999]) | |
# d10000 | |
d10000 = int(a[9999]) | |
# d100000 | |
d100000 = int(a[99999]) | |
# d1000000 | |
d1000000 = int(a[999999]) | |
# printing the solution | |
print d1*d10*d100*d1000*d10000*d100000*d1000000 | |
# time at the end of program execution | |
end = time.time() | |
# total time for execution | |
print end - start |
As always you can download the source code from Github Gist pep40.py
This solution also satisfies PEP8 standard.
Output
Summary
I don't really have any big comments on this problem. But this problem demanded some homework and usage of basic mathematics to make it optimized and work faster. Performance wise I am satisfied and I have not tried optimizing the code. The code I have presented to you is the first draft. You can write a better code and share it with me, if possible.As always if you have any doubt or didn't understand anything then you can comment in the comment box below, I will be glad to help you.
Please do comment if you have found any typo or have any better program or have a different program or have any suggestion. I will be glad to view each of them.
You can also contact me.
Thank you, have a nice day😃.