Project Euler > Problem 40 > Champernowne's constant (Java Solution)

Problem:

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


Solution:

210


Code:
The solution may include methods that will be found here: Library.java .

public interface EulerSolution{

public String run();

}
/* 
 * Solution to Project Euler problem 40
 * By Nayuki Minase
 * 
 * http://nayuki.eigenstate.org/page/project-euler-solutions
 * https://github.com/nayuki/Project-Euler-solutions
 */


public final class p040 implements EulerSolution {
 
 public static void main(String[] args) {
  System.out.println(new p040().run());
 }
 
 
 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(Library.pow(10, i) - 1) - '0';
  return Integer.toString(prod);
 }
 
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact