1

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!

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
RobCo
  • 61
  • 1
  • 4
  • just as a hint you should start at 9 and work your way down. Remember some of the math rules, if all digits added together are divisible by 3 then the entire number is divisible by 3, you can also use your prime factorization method and modify it to work with this problem. Also maybe start with all your digits in an array and get the lexographical permutation of those digits, instead of arbitrarily creating numbers – johnny 5 Jun 23 '16 at 13:59
  • Yes, absoutely correct, thank you for the input! – RobCo Jun 23 '16 at 14:57

1 Answers1

5

Arrays.asList(), when passed a primitive array, produces a List whose single element is the input array. If you pass to it an Integer[] instead of int[], it will behave the way you expected.

Just change

    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};

to

    Integer[] digits = new Integer[chars.length];
    for(int i = 0; i < chars.length; i++){
        digits[i] = chars[i] - '0';
    }
    Integer[] allDigits = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Alright, I honestly didn't know that. Your solution worked, thank you very much! – RobCo Jun 23 '16 at 14:33
  • 1
    @RobCo since that answer worked for you, it's probably a good idea to accept it by clicking the checkmark next to it. That will let others know both that you've found a solution and other people with the same issue will know what worked for you (and you and the answerer will each get some reputation for it too) – Eric Renouf Jun 23 '16 at 14:54
  • @EricRenouf Done, thank you for your advice friend. – RobCo Jun 28 '16 at 12:33