Euler Problem 62

This one is cool. I figured out two new algorithms that I’m sure will be handy later on.

1) Custom signatures. When comparing long lists, its sometimes easier to create a parallel list of representative tokens. This makes filtering, sorting, counting much easier.

2) Collections can be used to sort lists, but also to count duplicates in them. The frequency method is very fast and useful.

public String Problem ()
        ArrayList<String> cubeList = new ArrayList<String>();
        ArrayList<String> S = new ArrayList<String>();
        for (long i = 345; i <= 9999; i++)
            String cube = String.valueOf((long)i*i*i);
            String sig = "";
            int [] d = {0,0,0,0,0,0,0,0,0,0};
            for (int c = 0; c <= cube.length()-1; c++)
            for (int n = 0; n <= 9; n++)
                sig += d[n];
        String Five = "";
        for (String s:S)
            if (Collections.frequency(S, s) == 5)
                Five = s;
        return String.valueOf(cubeList.get(S.indexOf(Five)));

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s