Wednesday, August 6, 2014

Problem 26 - E#

So, E#...It's a fairly simple language with common curly brace syntax. However, I wouldn't call it quite the greatest language in the world. Things started looking not-the-best when I checked out the svn repository only to see that it was only in its second revision. Then, the E# source didn't actually compile, and I need to change a few headers in order to get it to do so. After that came trying to learn the language. As I said, the syntax was pretty simple and easy to pick up, but the "doc" folder of the distribution consists of just 9 short examples, with some comments. And then...the slowness. I wrote up solutions to 97, 91, and 53 in E#, hoping to get something done, but the language is so slow that things that are instant in python might take an hour in this language.

When I finally chose to solve problem 26 in E#, things started looking grim, as re-implementing my previous solution took over 10 minutes to search less than half of the space. However, I then realized that you can eliminate most of the search space in the problem by counting from the top down rather than the bottom up. Even with this time saving...it has to check fewer than 1000 things for each of fewer than 20 elements...I still couldn't technically get under a minute, but I got close enough on my not blazingly fast machine that I am willing to call this problem solved. Code runs in about 1m21s on my machine.
ans = 0;
clength = 0;
for (d = 999; d > clength; --d) {
    rems = list.new(0);
    r = 1;
    i = 0;
    while (r != 0) {
        r = (r * 10) % d;
        for (j = 0; j < i; ++j) {
            if (rems.get(j) == r) {
                r = 0;
                j = i;
            }
        }
        if (r != 0) {
            
            rems.append(r);
            i += 1;
        }
    }
    if (clength < i) {
        ans = d;
        clength = i;
    }
}

stdout.println(ans);

No comments:

Post a Comment