Solution to Project Euler 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 dn represents the nth digit of the fractional part, find the value of the following expression. d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000
Updated: Jan. 16, 2021 — Training Time: 2 minutes
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic: Project Euler Problem 40: Champernowne's constant.
Difficulty:Advance.
Objective: 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
Input: None.
Expected Output: 210.
Sikademy Solution in Java Programming Language
package sikademy;
/**
*
* @author Archangel Macsika
* Copyright (c) Sikademy. All rights reserved
*/
public class SikademyEulerSolution {
public String run() {
StringBuilder sb = new StringBuilder();
for (int i = 1; i < 1000000; i++)
sb.append(i);
int prod = 1;
for (int i = 0; i <= 6; i++)
prod *= sb.charAt(pow(10, i) - 1) - '0';
return Integer.toString(prod);
}
public static int pow(int x, int y) {
if (x < 0)
throw new IllegalArgumentException("Negative base not supported");
if (y < 0)
throw new IllegalArgumentException("Negative exponent");
int z = 1;
for (int i = 0; i < y; i++) {
if (Integer.MAX_VALUE / z < x)
throw new ArithmeticException("Overflow");
z *= x;
}
return z;
}
public static void main(String[] args) {
SikademyEulerSolution solution = new SikademyEulerSolution();
System.out.println(solution.run());
}
}
Sikademy Solution in Python Programming Language
#
# @author Archangel Macsika
# Copyright (c) Sikademy. All rights reserved.
#
def compute():
s = "".join(str(i) for i in range(1, 1000000))
ans = 1
for i in range(7):
ans *= int(s[10**i - 1])
return str(ans)
if __name__ == "__main__":
print(compute())