Created
April 18, 2017 00:39
-
-
Save LuxXx/e00ffffaa0e12448092b3eace2b71480 to your computer and use it in GitHub Desktop.
Project Euler - Problem 41
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
package euler; | |
public class Pandigital { | |
public static void main(String[] args) { | |
int n = 1_000_000_000; | |
// initially assume all integers are prime | |
boolean[] isPrime = new boolean[n+1]; | |
for (int i = 2; i <= n; i++) { | |
isPrime[i] = true; | |
} | |
for (int factor = 2; factor*factor <= n; factor++) { | |
// if factor is prime, then mark multiples of factor as non-prime | |
// suffices to consider multiples factor, factor+1, ..., n/factor | |
if (isPrime[factor]) { | |
for (int j = factor; factor*j <= n; j++) { | |
isPrime[factor*j] = false; | |
} | |
} | |
} | |
for (int i = 0; i < isPrime.length; i++) { | |
if (isPrime[i] && isPandigital(String.valueOf(i))) | |
System.out.println(i); | |
} | |
} | |
public static boolean isPandigital(String s) { | |
for (int i = 1; i <= s.length(); i++) { | |
if (!s.contains(String.valueOf(i))) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment