GolfScript, 25 2424 23 bytes
{:i;0.0\{@@).2$?.i<-1$<}do@;@}
I’m pretty sure this method is optimal, considering GolfScript doesn’t have floats and thus no min/max. It’s possible that there’s a shorter wayIt’s possible that there’s a shorter way to do the stack manipulation, but that seems pretty unlikely to me at this point. I stand corrected (by myself)—I was able to doget rid of the stack manipulation, but that seems pretty unlikely to me at this pointvariable i
. Now I think this is optimal…but then I’ll probably find another 1-byte save somewhere else 😂
This is a block (function) taking N then P on the stack and leaving the two values on top of the stack.
Explanation:
Code | Stack (bottom to top) |
---|---|
{…} Block |
2 17 |
:i; Assign to i |
2 |
0.0\ |
2 0 017 |
{…}do Run until result is 0… |
Showing first pass over: |
@@ Stack shift |
-117 2 0 |
) Increment |
-117 2 1 |
: Duplicate |
-117 2 1 1 |
2$ Push third from stack |
-117 2 1 1 2 |
? Exponentiate |
-117 2 1 2 |
: Duplicate |
-17 2 1 2 2 |
-1$ Push bottom of stack |
17 2 1 2 2 17 |
i<< Less than i ? |
-1 2 1 2 0 |
(pop and either repeat or stop) | -117 2 1 2 |
At the end of the loop, the stack looks like 017 1 4 9 16 2 5 25
so we do @;@
to make the top of the stack be 25 16
.
It took like twenty minutes of fiddling around to get the stack manipulation to work right.