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);
            cubeList.add(cube);
            String sig = "";
            int [] d = {0,0,0,0,0,0,0,0,0,0};
            for (int c = 0; c <= cube.length()-1; c++)
                d[Integer.parseInt(cube.charAt(c)+"")]++;
            for (int n = 0; n <= 9; n++)
                sig += d[n];
            S.add(sig);
        }
        String Five = "";
        for (String s:S)
            if (Collections.frequency(S, s) == 5)
            {
                Five = s;
                break;
            }
        return String.valueOf(cubeList.get(S.indexOf(Five)));
    }

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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