I'm currently trying to solve Problem 38 of Project Euler and although I obviously try to solve every problem myself I seem to have hit a brick wall and I think I need your help for this one.
So here's my code:
import java.util.Arrays;
public class PandigitalMultiples{
public static void main(String[]args){
long c = 0;
long pdLong = 0;
for(int i = 1; i < 10000; i ++){
String pdString = "";
for(int n = 1; pdString.length() <= 9; n++){
long p = n * i;
String pString = String.valueOf(p);
pdString = pdString + pString;
pdLong = Long.parseLong(pdString);
if(is1To9Pandigital(pdString) && pdLong > c && pdLong < 1000000000 && n > 1){
c = pdLong;
}
}
}
System.out.println(c);
}
public static boolean is1To9Pandigital (String pNumber){
int length = pNumber.length();
if(length != 9){
return false;
}
char[]chars = pNumber.toCharArray();
int[]digits = new int[chars.length];
for(int i = 0; i < chars.length; i++){
digits[i] = chars[i] - '0';
}
int[]allDigits = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
boolean contains = Arrays.asList(digits).containsAll(Arrays.asList(allDigits));
if(contains == true){
System.out.println(pNumber);
return true;
}
else
return false;
}
}
The problem seems to be that even for numbers like 918273645 Arrays.asList(digits).containsAll(Arrays.asList(allDigits))
returns false and I don't know why!
Maybe someone of you can help me? I would really appreciate it!