Created
April 21, 2017 23:23
-
-
Save LuxXx/5e6185db7bbeb499c2058171f7f8df79 to your computer and use it in GitHub Desktop.
Project Euler - Problem 40
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Champernowne { | |
private static String num = null; | |
public static void main(String[] args) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < 1_000_000; i++) { | |
sb.append(String.valueOf(i)); | |
if (i == 0) sb.append('.'); | |
} | |
num = sb.toString(); | |
int n = d(1); | |
for (int i = 1; i < 7; i++) { | |
int pow = (int) Math.pow(10, i); | |
n *= d(pow); | |
} | |
System.out.println(n); | |
} | |
public static int d(int n) { | |
if (num == null) throw new Error("num is null"); | |
return num.charAt(n + 1) - 48; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment