Tuesday, August 5, 2014

Lots of difficulties, but here is problem 97

I am trying to get a lot of problems done - I have done 95 problems, and this Saturday, August 9th, will mark the 1-year anniversary of the hackathon when I began this blog. My goal is to try to get to 100 problems solved by then. The past couple of days, I have hit a couple of road blocks in moving towards that goal. First, I tried to solve problem 96 (Sudoku puzzles) in  language called Pike, but ran into issues with Pike seemingly not agreeing with its own language spec. I am now working on a Java solution to 96, but still have to work out details. I then decide to move beyond problem 96 and look at problem 97. I found a language called E# that seemed like a pretty solid language, but it was SLOW. slower than Io, which I complained about being slow a while ago. We are talking at least 2 orders of magnitude slower than Python slow. It was slow. So, despite writing solutions to problem 97 and a couple of others in E#, I was not able to find a problem which E# could solve in a human time-scale. So, as not to have nothing to show for the past couple of days, here is my solution to problem 97 in Pike. It's not super exciting, not even super optimized (I could go a few powers of two at a time, instead of just multiplying by 2). But, here we go. Code runs in about 10s on my machine.
int main() {
    array(int) digits = ({3, 3, 4, 8, 2, 0, 0, 0, 0, 0});

    
    for (int n = 0; n < 7830457; ++n) {
        int carry = 0;
        for (int i = 0; i < 10; ++i) {
            int prod = (digits[i] * 2) + carry;
            carry = prod / 10;
            digits[i] = prod % 10;
        }
    }
    digits[0]++;
    for (int i = 9; i >= 0; --i) {
        Stdio.stdout->printf("%d", digits[i]);
    }
    Stdio.stdout->printf("\n");
}

No comments:

Post a Comment