This means that after 4 operations, the output is the starting value. Obvious, but also important.

If we can find 2 numeric measurable quanteties that each contain exactly 1 bit of information, we can use this to cycle through the 4 stages, since it takes exactly 2 bits to count to 4. Let’s call these 2 bits S and E, for reasons that will be clear later.

We also need to satisfy the property, that any number mapped through one of the map functions, will change the output of either E(x), S(x) or both, such that a number mapped through all 4 stages will hit the conditions outlined in “is_stage1” through “is_stage4”.

Now we need to think of 2 measurable 1-bit quantities on whole numbers. One is pretty easy: the sign bit. The number is either positive or negative (0 counts as positive here). We’ll call the sign bit test S(x). The other one can be anything we like, but an easy one is whether the number is odd or even. We’ll call this E(x). Now the original naming should be appearant.

And if you want to see it in action, try this extra code. It demonstrates what happens around the limits of an 8-bit integer, which is interesting, since throwing 2-complements notation into the mix makes the problem even harder. My answer does NOT take this into account, for simplicity. Bonus problem: Can you explain why the reality of 2-complement notation makes this problem harder around the edge cases? Can it be fixed? If so, how?

You return the Real value at 0 and 90 degrees, and the negative value of the Real value at any other angle. That seems wrong.

Also, the “f” function in the alternate-version is now basically a counter, which also goes against the spirit of the question.

A proper no-tricks solution should operate only on an integer, with no other state being involved.

It’s true that packing a complex number into 2 integers can sort of do that, but you could also just say that the top 2 bits are reserved for counting how many times the number has been through f(). That’s basically a bit of a cheat, I think 😉