Euler Problem 59

There are two key points here. The key consists of lower case letters, so although its a brute force attack you can reduce the scope to speed it up. The second is when testing the resulting string, search for multiple occurences of the most common English words.

public String Problem ()
    {
        String cipher = "", line = "";
        int key [] = new int [3];
        int cycle = 0, result = 0;
        try
        {
            FileInputStream stream = new FileInputStream("F:/Development/Java/EULER/src/euler/cipher1.txt");
            DataInputStream in = new DataInputStream(stream);
            BufferedReader buffer = new BufferedReader(new InputStreamReader(in));
            line = buffer.readLine();
            in.close();
        }
        catch (Exception e)
        {
              System.out.print(e+"\n");
        }
        MAIN:
        for (key[0] = 97; key[0] <= 122; key[0]++)
        {
            for (key[1] = 97; key[1] <= 122; key[1]++)
            {
                for (key[2] = 97; key[2]  5 && numOccur(cipher,"and") > 3)
                    {
                        System.out.format ("[0][1][2]:[%d][%d][%d]:%s [...]\n", key[0],key[1],key[2],cipher.substring(0, 100));
                        break MAIN;
                    }
                }
            }
        }
        for (int c = 0; c <= cipher.length()-1; c++)
        {
           result += cipher.charAt(c);
        }
        return String.valueOf(result);
    }
    public int numOccur (String source, String target)
    {
        int count = 0;
        int start = source.indexOf(target);
        while (start != -1)
        {
            count++;
            start = source.indexOf(target, start+target.length());
        }
        return count;
    }

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