December 29, 2015

Euler 40 for Java: Finding the nth digit of the fractional part of the irrational number

Simply, the Integer array can be used to write digits.
public class Test {
  static int[] x = new int[1000000];
  static int index = 0;
  public static void main(String[] args) {
    for (int i = 1; i < x.length; i++) {
      if(index >= x.length)
        break;
      
      append(i);
    }
    
    System.out.println(d(1) * d(10) * d(100) * d(1000) * d(10000) * d(100000) * d(1000000));
  }
  
  private static int d(int i) {
    return x[i - 1];
  }
  
  private static void append(int n) {
    String a = String.valueOf(n);
    for(int i = 0; i < a.length(); i++) {
      if(index >= x.length)
        break;
      
      x[index] = Integer.valueOf(String.valueOf(a.charAt(i)));
      index++;
    }
  }
}

No comments:

Post a Comment