Skip to content

Instantly share code, notes, and snippets.

@LuxXx
Created April 18, 2017 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuxXx/e00ffffaa0e12448092b3eace2b71480 to your computer and use it in GitHub Desktop.
Save LuxXx/e00ffffaa0e12448092b3eace2b71480 to your computer and use it in GitHub Desktop.
Project Euler - Problem 41
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