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