# Introduction

Finding the closest power to a number is a common enough problem. But what if you need both the next-highest and next-lowest power? In this challenge you must find the closest powers to a given number - the 'power sandwich' if you will, where the given number is the filling and the powers are the bread. Mmm, tasty.

# Challenge

Given a power P >0 and a number N >0, output the largest integer x^P that is smaller or equal to N, and the smallest integer y^P that is greater or equal to N.

Input should be taken as a list of two positive (>0) integers, first the power P and then the number N. Output should be a list of two integers, the first being smaller or equal to N, the second being greater or equal to N, and both being a power of P.

If N is a power of P already, the output should be the list [N, N].

This is , so the shortest code (as measured in bytes) wins.

# Example Input and Output

Input:

[2, 17]

Output:

[16, 25]

Explanation: 16 is the biggest square number (power of 2) less than or equal to 17, and 25 is the smallest square number greater or equal to 17.

# Test cases

[2, 24] -> [16, 25]
[2, 50] -> [49, 64]
[3, 8] -> [8, 8]
[1, 25] -> [25, 25]
[3, 25] -> [8, 27]
[4, 4097] -> [4096, 6561]
[2, 10081] -> [10000, 10201]
[11, 2814661] -> [177147, 4194304]
[6, 1679616] -> [1000000, 1771561]

• I think the output for the last test case should be [ 1000000, 1771561 ]. Nice first challenge, anyway! Commented Oct 27, 2023 at 14:41
• I think a more interesting challenge might be to find the sandwich without restricting p. Commented Oct 27, 2023 at 15:35
• I think you mean both outputs should be a Pᵗʰ power.
– Neil
Commented Oct 27, 2023 at 15:52
• @Jonah I think Neil's comment was just for the OP. It's completely unrelated to your suggestion. (The challenge does indeed mention "power of P", which is wrong.) Commented Oct 27, 2023 at 17:59
• @Jonah I made the challenge that you proposed: codegolf.stackexchange.com/questions/266293/… Commented Oct 29, 2023 at 1:37

# JavaScript (ES7), 39 bytes

Expects (p)(n).

p=>n=>[x=(q=n**(1/p)|0)**p,(q+=x<n)**p]


Try it online!

### Alternate version (same size)

p=>n=>[0,1].map(i=>(n--**(1/p)+i|0)**p)


Try it online!

• Fail 3,64 due to float precision
– l4m2
Commented Oct 27, 2023 at 18:13

# R, 3635 33 bytes

\(P,N)abs(c(r<-N^(1/P),-r)%/%1)^P


Attempt This Online!

# Vyxal, 7 bytes

Ėe₍⌊⌈$e  Try it Online! Trivial implementation. Ėe₍⌊⌈$e­⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁪‏‏​⁡⁠⁡‌­
Ėe       # ‎⁡Exponentiate N with the reciprocal of P
₍⌊⌈    # ‎⁢Pair results of floor, ceil in a list
$e # ‎⁣Swap (returns first input) and exponentiate with P 💎  # Python, 46 bytes based on port of Arnauld's JavaScript answer lambda p,n:[int(q:=n**(1/p))**p,(-(-q//1))**p]  Attempt This Online! floor and ceil of p-th root to the power of p # BQN, 10 bytes (⌊⋈⌈)∘√´⋆⊑  Try it here. Explanation: (⌊⋈⌈)∘√´⋆⊑ # tacit function which takes a list [P,N] ⌊⋈⌈ # take the floor paired with the ceil ( )∘√´ # of the P-th root of N ⋆⊑ # and raise to the power P  # Desmos, 51 bytes f(l)=[floor(l[2]^{1/l})^l[1],ceil(l[2]^{1/l})^l[1]]  Try it on Desmos! Expects list [p,n] and returns list [x^p,y^p] as per specifications. Explanation: f(l)=[ , ] Given a list, return a list floor(l[2]^{1/l})^l[1] floor(n^1/p)^p ceil(l[2]^{1/l})^l[1] ceil(n^1/p)^p  • You can definitely save some bytes by putting l[2]^{1/l} into a wackscope variable, along with putting ^l[1] outside the list to take advantage of Desmos's broadcasting. But at that point, that starts to converge towards my answer. Commented Oct 29, 2023 at 18:30 # Jelly, 8 bytes *İ}Ḟ,ĊƊ*  Try it online! Profoundly boring direct floor-ceil solution. However, if the N-is-a-power-of-P case can be relaxed to permit outputting just [N]... # Jelly, 7 bytes *İ}¹ị*€  Try it online! ...we actually have a builtin for floor-ceil. Namely, the index-into dyad ị. *İ} Pth root of N. ¹ (Break undesired initial 2,2,2 chain.) ị Index that into *€ the list of x^P for all x <- 1..N.  # Desmos, 37 bytes f(p,n)=[floor(k),ceil(k)]^p k=n^{1/p}  Try It On Desmos! Try It On Desmos! - Prettified The best I could think of for now. # 05AB1E, 8 bytes zmDî‚ï¹m  Port of @AidenChow's Desmos answer. Inputs in the order $$\P,N\$$. Explanation: $$[N_a,N_b]=\left[\left\lceil N^\frac{1}{P}\right\rceil,\left\lfloor N^\frac{1}{P}\right\rfloor\right]$$ z # Push 1/P, where P is the first (implicit) input-integer m # Take N to the power this 1/P, where N is the second (implicit) input-integer D # Duplicate it î # Ceil the copy ‚ # Pair the two together ï # Floor/cast both to integers ¹m # Take both values in the pair to the power P # (after which the resulting pair is output implicitly)  # Charcoal, 29 bytes ＮθＮη≔¹ζＷ‹Ｘζθη≦⊕ζＩＸ⟦⁻ζ›Ｘζθηζ⟧θ  Try it online! Link is to verbose version of code. Explanation: ＮθＮη  Input P and N. ≔¹ζ  Start with y=1. Ｗ‹Ｘζθη≦⊕ζ  Increment y until it is large enough. ＩＸ⟦⁻ζ›Ｘζθηζ⟧θ  Output the Pth powers of x and y. 61 bytes for a more efficient version: ＮθＮη≔¹ζ≔¹εＷ‹Ｘεθη≦⊗εＷ›⁻εζ¹«≔÷⁺εζ²ιＦ¬‹Ｘιθη≔ιεＦ¬›Ｘιθη≔ιζ»ＩＸ⟦ζε⟧θ  Try it online! Link is to verbose version of code. Explanation: ＮθＮη  Input P and N. ≔¹ζ≔¹ε  Start with both x and y equal to 1. Ｗ‹Ｘεθη≦⊗ε  Double y until it is large enough. Ｗ›⁻εζ¹«≔÷⁺εζ²ιＦ¬‹Ｘιθη≔ιεＦ¬›Ｘιθη≔ιζ»  Perform a binary search to narrow down the range of x and y. ＩＸ⟦ζε⟧θ  Output the Pth powers of x and y. 19 bytes using floating-point arithmetic: Ｎθ≔ＸＩη∕¹θηＩＸ⟦⌊η⌈η⟧θ  Try it online! Link is to verbose version of code. Explanation: Ｎθ  Input P. ≔ＸＩη∕¹θη  Take the Pth root of N. ＩＸ⟦⌊η⌈η⟧θ  Output the Pth powers of the floor and ceiling. # GolfScript, 2524 23 bytes {0\{@@).2$?.-1$<}do@;@}  Try it online! 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 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 get rid of the variable 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 0\ 2 0 17 {…}do Run until result is 0… Showing first pass over: @@ Stack shift 17 2 0 ) Increment 17 2 1 : Duplicate 17 2 1 1 2$ Push third from stack 17 2 1 1 2
? Exponentiate 17 2 1 2
: Duplicate 17 2 1 2 2
-1$ Push bottom of stack 17 2 1 2 2 17 < Less than? -1 2 1 2 0 (pop and either repeat or stop) 17 2 1 2 At the end of the loop, the stack looks like 17 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. • When the input is already a perfect power then the output needs to be two copies of the input, e.g. the 3, 8 example. – Neil Commented Oct 28, 2023 at 8:39 # C#, 79 bytes (p,n)=>(Math.Pow(Math.Pow(n,1d/p)is{}x?(int)x:x,p),Math.Pow(Math.Ceiling(x),p))  Alternate approach that uses 87 bytes (p,n)=>{var(i,x)=(0,0d);for(;(x=Math.Pow(++i,p))<n;);return(Math.Pow(x==n?i:~-i,p),x);}  Try it online! # APL+ WIN, 18 bytes Prompts for power and integer: ((⌊p),⌈p←⎕*÷n)*n←⎕  Try it online! Thanks to Dyalog Classic # MATL, 14 bytes w1Y\^tkwXkh1G^  w % Get both inputs and bring P to the top of the stack 1Y\ % Take the inverse of P ^ % Raise N to that power i.e. take N's P-th root tk % Take a copy of that root and floor it wXk % And ceil the original copy h1G^ % Raise both those values to the power of P  # Vyxal 3, 7 bytes ė*∦⌊⌈$*


Try it Online!

Takes two integers as its argument and returns two integers. Works the same as various other answers including @mathscat’s Vyxal one and @UnrelatedString’s Jelly one.

## Explanation

ė*∦⌊⌈$* ė | Reciprocal of n * | To the power of p ∦⌊⌈ | Pair ceiling with floor and wrap in a list$* | To the power of p

$$$$


# Rust, 67 66 bytes

|p,n:f64|{let x=n.powf(1./p);(x.floor().powf(p),x.ceil().powf(p))}


Try it online!

Trivial implementation: takes n to the power of 1/p, and then calculates floor and ceil to the power of p`.

Takes the integer inputs as 64-bit floats and returns its output as a tuple of 64-bit floats.

-1 byte thanks to ceilingcat

• @ceilingcat thanks, I completely missed that one Commented Dec 21, 2023 at 1:47