Problem 40
Champernowne’s constant
An irrational decimal fraction is created by concatenating the positive integers:
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.
钱珀瑙恩常数
将所有正整数连接起来构造的一个十进制无理数如下所示:
可以看出小数点后第12位数字是1。
如果dn表示上述无理数小数点后的第n位数字,求下式的值:
@Test
public void test() {
int d1 = getBitVal(1);
int d10 = getBitVal(10);
int d100 = getBitVal(100);
int d1000 = getBitVal(1000);
int d10000 = getBitVal(10000);
int d100000 = getBitVal(100000);
int d1000000 = getBitVal(1000000);
System.out.println(d1 * d10 * d100 * d1000 * d10000 * d100000
* d1000000);
}
public int getBitVal(int id) {
int numBit = 0;
int sumBit = 0;
while (id > sumBit) {
numBit += 1;
sumBit += Math.pow(10, numBit - 1) * numBit * 9;
}
int start = (int) Math.pow(10, numBit - 1);
int rest = (int) (id - sumBit + Math.pow(10, numBit - 1) * numBit * 9);
int ret = 0;
ret = start + rest / numBit;
if (rest % numBit == 0) {
ret -= 1;
}
int bitRest = rest % numBit == 0 ? numBit - 1 : rest % numBit - 1;
return Integer.valueOf(String.valueOf("0"
+ Integer.toString(ret).charAt(bitRest)));
}