Challenge

Make this iterative:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
typedef std::function<int()> N;

int A(int k, const N &x1, const N &x2, const N &x3, const N &x4, const N &x5){
    if (k <= 0)
        return x4() + x5();
    N B = [&](){
        k--;
        return A(k, B, x1, x2, x3, x4);
    };
    return B();
}

N F(int n){
    return [=](){ return n; };
}

int A(int k, int x1, int x2, int x3, int x4, int x5){
    return A(k, F(x1), F(x2), F(x3), F(x4), F(x5));
}

A(16, 1, -1, -1, 1, 0);
Tricky.

BTW, it never terminates.
Plot Twist: Helios gives us his work in disguise of a challenge.
Plot Twist: Helios gives us his her work in disguise of a challenge.
What do you mean, it never terminates?
helios wrote:
What do you mean, it never terminates?

It probably just blew his stack, as it did mine before I enlarged it (with ulimit). I hope you post the answer at some point (but not yet!).
Oh, I didn't see this until now. Looks interesting. I assume the constraint is that we have to keep the interface to A(6x int) unchanged?
Last edited on
Err... Sure, although it doesn't make it much easier if you don't.
A(50, 1, -1, -1, 1, 0) = -4357661238744040
A(100, 1, -1, -1, 1, 0) = -920829370818316202730797688469299
Last edited on
Did you find a stack-free solution? My computer ran out of memory at around k=27.
Yes, but I basically cheated.
I never would've figured it out myself.
I'll PM you the solution and how I found it.
Topic archived. No new replies allowed.