Problem Description
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
Analysis
This is known as Champernowne’s constant (OEIS A033307).
We concatenate the counting numbers to a string from 1 until the length reaches at least 1 million digits, then multiply the digit at the appropriate positions together. We know from the problem description that the first two terms d1 & d10 will evaluate to 1 and, therefore, serve no consequence on the product.
Solution
Runs < 1 second in Perl.
my $s = '.'; my $n = 1; $s.=$n++ while(length($s) <= 1_000_000); print "Answer to PE40 = ", substr($s,1e2,1) * substr($s,1e3,1) * substr($s,1e4,1) * substr($s,1e5,1) * substr($s,1e6,1);
Comments
- We initialize $s with a character to start the string index at 1 instead of 0.
- Mathematica: Times @@ Flatten[IntegerDigits@Range@2*^5][[10^Range@6]]





Discussion
No comments for “Project Euler Problem 40 Solution”