project euler 40

Problem 40


Champernowne’s constant

An irrational decimal fraction is created by concatenating the positive integers:

0.12345678910 1112131415161718192021…

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.

d 1 × d 10 × d 100 × d 1000 × d 10000 × d 100000 × d 1000000

钱珀瑙恩常数

将所有正整数连接起来构造的一个十进制无理数如下所示:

0.12345678910 1112131415161718192021…

可以看出小数点后第12位数字是1。

如果dn表示上述无理数小数点后的第n位数字,求下式的值:

d 1 × d 10 × d 100 × d 1000 × d 10000 × d 100000 × d 1000000

@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)));

	}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值