이경수 선생님의 수학실험실

Problem 40(Champernowne's constant) 본문

Project Euler

Problem 40(Champernowne's constant)

(이경수) 2019. 6. 16. 14:04

Problem 40(Champernowne's constant)

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 \(d_{n}\) 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}\)

 

In Python:

import time

startTime = time.time()
digitList = []
orderList = [1, 10, 10 ** 2, 10 ** 3, 10 ** 4, 10 ** 5, 10 ** 6]
product = 1

for n in range(1, 10 ** 6):
    numList = list(str(n))
    for num in numList:
        digitList.append(int(num))

for order in orderList:
    product *= digitList[order - 1]

print(product)
print(time.time() - startTime, "seconds")

Run time: 2.7522952556610107 seconds

 

In Java:

package project_euler31_40;

import java.util.ArrayList;

public class Euler40 {
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		int product = 1;
		int[] orderList = {1, 10, (int)Math.pow(10, 2), 
				(int)Math.pow(10, 3), (int)Math.pow(10, 4), 
				(int)Math.pow(10, 5), (int)Math.pow(10, 6)};
		ArrayList<Integer> digitList = new ArrayList<Integer>();
		String nStr = "";
		String nData[];
		
		for (int n = 1; n < (int)Math.pow(10, 6); n++) {
			nStr = Integer.toString(n);
			nData = nStr.split("");
			for (int i = 0; i < nData.length; i++) {
				digitList.add(Integer.parseInt(nData[i]));
			}
		}
		for (int order : orderList) {
			product *= digitList.get(order - 1);
		}
		System.out.println(product);
		long endTime = System.currentTimeMillis();
		System.out.println((double)(endTime - startTime)/(double)1000 + "seconds");
	}
}

Run time: 1.571seconds

 

Solution: 210

'Project Euler' 카테고리의 다른 글

Problem 42(Coded triangle numbers)  (0) 2019.07.24
Problem 41(Pandigital prime)  (0) 2019.06.16
Problem 39(Integer right triangles)  (0) 2019.06.16
Problem 38(Pandigital multiples)  (0) 2019.06.15
Problem 37(Truncatable primes)  (0) 2019.06.06
Comments