1
+ /*
2
+ This problem can also be solved using math...but I made a bashy program solution anyways since it's like 5 lines of code
3
+ Also made a less bashy program solution using the following algorithm because I'm bored
4
+
5
+ By concatenating all x-digit long natural numbers, we will obtain a string x * 9 * 10^(n-1) characters long
6
+
7
+ Let c(x) be a function that returns the length of the string obtained by concatenating all x-digit long natural numbers:
8
+
9
+ c(0): 0-character long string (duh)
10
+ c(1): 9-character long string
11
+ c(2): 180-character long string
12
+ c(3): 2700-character long string
13
+ c(4): 36000-character long string
14
+ c(5): 450000-character long string
15
+ c(6): 5400000-character long string
16
+
17
+ Thus, to find the nth digit in C_10 (Base 10 Champernowne's Constant), we can use the following algorithm:
18
+ Starting at k = 0, subtract c(k) from n until n' <= c(k+1). Then, nth digit is contained in the ceil(n' / (k+1))-th k+1-digit number.
19
+ Index of nth digit in the number will be equal to n' % k+1. (If 0 then (n' % k+1)-th position)
20
+
21
+ 1st digit: 1 - c(0) = 1 = n' <= c(1). 1st digit is contained in the ceil(1 / 1) = 1st 1-digit number = 10^(1-1) + 1 - 1 = "1". Index of digit in "1" = 1 % 1 = 0, so 1st position.
22
+ Therefore, 1st digit = 1.
23
+ 10th digit: 10 - c(0) - c(1) = 1 = n' <= c(2). 10th digit is contained in the ceil(1 / 2) = 1st 2-digit number = 10^(2-1) + 1 - 1 = "10". Index of digit in "10" = 1 % 2 = 1, so 1st position.
24
+ Therefore, 10th digit = 1.
25
+ 100th digit: 100 - c(0) - c(1) = 91 = n' <= c(3). 100th digit is contained in the ceil(91 / 2) = 46th 2-digit number = 10^(2-1) + 46 - 1 = "55". Index of digit in "55" = 91 % 2 = 1, so 1st position.
26
+ Therefore, 100th digit = 5.
27
+ 1000th digit: 1000 - c(0) - c(1) - c(2) = 811 = n' <= c(3). 1000th digit is contained in the ceil(811 / 3) = 271th 3-digit number = 10^(3-1) + 271 - 1 = "370". Index of digit in "370" = 811 % 3 = 1, so 1st position.
28
+ Therefore, 1000th digit = 3.
29
+ 10000th digit: 10000 - c(0) - c(1) - c(2) - c(3) = 7111 = n' <= c(4). 10000th digit is contained in the ceil(7111 / 4) = 1778th 4-digit number = 10^(4-1) + 1778 - 1 = "2777". Index of digit in "2777" = 7111 % 4 = 3, so 3rd position.
30
+ Therefore, 10000th digit = 7.
31
+ 100000th digit: 100000 - c(0) - c(1) - c(2) - c(3) - c(4) = 61111 = n' <= c(5). 100000th digit is contained in the ceil(61111 / 5) = 12223th 5-digit number = 10^(5-1) + 12223 - 1 = "22222". Index of digit in "22222" = 61111 % 5 = 1, so 1st position.
32
+ Therefore, 100000th digit = 2.
33
+ 1000000th digit: 1000000 - c(0) - c(1) - c(2) - c(3) - c(4) - c(5) = 511111 = n' <= c(6). 1000000th digit is contained in the ceil(511111 / 6) = 85186th 6-digit number = 10^(6-1) + 85186 - 1 = 185185. Index of digit in "185185" = 511111 % 6 = 1, so 1st position.
34
+ Therefore, 1000000th digit = 1.
35
+
36
+ Thus, the answer is 1 * 1 * 5 * 3 * 7 * 2 * 1 = 210.
37
+ */
38
+
39
+
40
+
41
+ //Bashy solution:
42
+
43
+ let champernownesConstant = "" ;
44
+
45
+ for ( let i = 1 ; i <= 500000 ; i ++ ) { //string should definitely be at least a million characters long now
46
+ champernownesConstant += i ;
47
+ }
48
+
49
+ let answer = parseInt ( champernownesConstant . charAt ( 1 - 1 ) ) * parseInt ( champernownesConstant . charAt ( 10 - 1 ) ) * parseInt ( champernownesConstant . charAt ( 100 - 1 ) ) * parseInt ( champernownesConstant . charAt ( 1000 - 1 ) ) * parseInt ( champernownesConstant . charAt ( 10000 - 1 ) ) * parseInt ( champernownesConstant . charAt ( 100000 - 1 ) ) * parseInt ( champernownesConstant . charAt ( 1000000 - 1 ) ) ;
50
+ console . log ( answer ) ;
51
+
52
+
53
+
54
+
55
+
56
+ //Algorithmic solution:
57
+
58
+ //function that returns the nth digit in C (Champernowne's Constant in Base 10) using the algorithm described at the top of the code
59
+ function nthDigitInC ( n ) {
60
+ let k = 0 ;
61
+ let m = n ;
62
+
63
+ // "Starting at k = 0, subtract c(k) from n until n' <= c(k+1)." (here we're just calling n' "m")
64
+
65
+ while ( m > lengthOfStringOfAllXDigitNumbersConcatenated ( k + 1 ) ) {
66
+ m -= lengthOfStringOfAllXDigitNumbersConcatenated ( k + 1 ) ;
67
+ k ++ ;
68
+ }
69
+
70
+
71
+
72
+ // "Then, nth digit is contained in the ceil(n' / (k+1))-th k+1-digit number."
73
+
74
+ let numberDigitIsContainedIn = ( 10 ** ( k ) ) + Math . ceil ( m / ( k + 1 ) ) - 1 ;
75
+
76
+ // "Index of nth digit in the number will be equal to n' % k+1. (If 0 then (n' % k+1)-th position)"
77
+
78
+ let index = m % ( k + 1 ) ;
79
+
80
+ if ( index === 0 ) {
81
+ return numberDigitIsContainedIn . toString ( ) . charAt ( index ) ;
82
+ } else {
83
+ return numberDigitIsContainedIn . toString ( ) . charAt ( index - 1 ) ;
84
+ }
85
+ }
86
+
87
+ //helper function that returns the returns the length of the string obtained by concatenating all x-digit long natural numbers
88
+ function lengthOfStringOfAllXDigitNumbersConcatenated ( x ) {
89
+ return ( x * 9 * ( 10 ** ( x - 1 ) ) )
90
+ }
91
+
92
+ let answer2 = 1 ; //accumulator kinda
93
+
94
+ for ( let i = 0 ; i <= 6 ; i ++ ) {
95
+ answer2 *= nthDigitInC ( 10 ** i ) ;
96
+ }
97
+
98
+ console . log ( answer2 ) ;
0 commit comments