Challenge

Nov 29, 2019 at 2:17pm
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);
Dec 9, 2019 at 1:21am
Tricky.

BTW, it never terminates.
Dec 9, 2019 at 3:20am
Plot Twist: Helios gives us his work in disguise of a challenge.
Dec 9, 2019 at 3:45am
Plot Twist: Helios gives us his her work in disguise of a challenge.
Dec 9, 2019 at 6:51am
What do you mean, it never terminates?
Dec 9, 2019 at 10:23pm
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!).
Dec 10, 2019 at 3:19am
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 Dec 10, 2019 at 3:20am
Dec 10, 2019 at 4:19am
Err... Sure, although it doesn't make it much easier if you don't.
Dec 10, 2019 at 5:26am
A(50, 1, -1, -1, 1, 0) = -4357661238744040
A(100, 1, -1, -1, 1, 0) = -920829370818316202730797688469299
Last edited on Dec 10, 2019 at 5:36am
Dec 10, 2019 at 6:49am
Did you find a stack-free solution? My computer ran out of memory at around k=27.
Dec 10, 2019 at 3:35pm
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.