15
\$\begingroup\$

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]
\$\endgroup\$
7
  • \$\begingroup\$ I think the output for the last test case should be [ 1000000, 1771561 ]. Nice first challenge, anyway! \$\endgroup\$
    – Arnauld
    Oct 27, 2023 at 14:41
  • 6
    \$\begingroup\$ I think a more interesting challenge might be to find the sandwich without restricting p. \$\endgroup\$
    – Jonah
    Oct 27, 2023 at 15:35
  • 5
    \$\begingroup\$ I think you mean both outputs should be a Pᵗʰ power. \$\endgroup\$
    – Neil
    Oct 27, 2023 at 15:52
  • 4
    \$\begingroup\$ @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.) \$\endgroup\$
    – Arnauld
    Oct 27, 2023 at 17:59
  • 1
    \$\begingroup\$ @Jonah I made the challenge that you proposed: codegolf.stackexchange.com/questions/266293/… \$\endgroup\$ Oct 29, 2023 at 1:37

16 Answers 16

6
\$\begingroup\$

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!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Fail 3,64 due to float precision \$\endgroup\$
    – l4m2
    Oct 27, 2023 at 18:13
5
\$\begingroup\$

R, 36 35 33 bytes

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

Attempt This Online!

\$\endgroup\$
4
\$\begingroup\$

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
💎
\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Aiden Chow
    Oct 29, 2023 at 18:30
2
\$\begingroup\$

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.
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 8 bytes

zmDî‚ï¹m

Port of @AidenChow's Desmos answer.

Inputs in the order \$P,N\$.

Try it online or verify all test cases.

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)
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 29 bytes

NθNη≔¹ζW‹Xζθη≦⊕ζIX⟦⁻ζ›Xζθηζ⟧θ

Try it online! Link is to verbose version of code. Explanation:

NθNη

Input P and N.

≔¹ζ

Start with y=1.

W‹Xζθη≦⊕ζ

Increment y until it is large enough.

IX⟦⁻ζ›Xζθηζ⟧θ

Output the Pth powers of x and y.

61 bytes for a more efficient version:

NθNη≔¹ζ≔¹εW‹Xεθη≦⊗εW›⁻εζ¹«≔÷⁺εζ²ιF¬‹Xιθη≔ιεF¬›Xιθη≔ιζ»IX⟦ζε⟧θ

Try it online! Link is to verbose version of code. Explanation:

NθNη

Input P and N.

≔¹ζ≔¹ε

Start with both x and y equal to 1.

W‹Xεθη≦⊗ε

Double y until it is large enough.

W›⁻εζ¹«≔÷⁺εζ²ιF¬‹Xιθη≔ιεF¬›Xιθη≔ιζ»

Perform a binary search to narrow down the range of x and y.

IX⟦ζε⟧θ

Output the Pth powers of x and y.

19 bytes using floating-point arithmetic:

Nθ≔XIη∕¹θηIX⟦⌊η⌈η⟧θ

Try it online! Link is to verbose version of code. Explanation:

Nθ

Input P.

≔XIη∕¹θη

Take the Pth root of N.

IX⟦⌊η⌈η⟧θ

Output the Pth powers of the floor and ceiling.

\$\endgroup\$
1
\$\begingroup\$

GolfScript, 25 24 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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Neil
    Oct 28, 2023 at 8:39
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

APL+ WIN, 18 bytes

Prompts for power and integer:

((⌊p),⌈p←⎕*÷n)*n←⎕

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
1
\$\begingroup\$

MATL, 14 bytes

w1Y\^tkwXkh1G^

Try on MATL Online or Test all cases

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
\$\endgroup\$
1
\$\begingroup\$

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

```
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ @ceilingcat thanks, I completely missed that one \$\endgroup\$
    – cg909
    Dec 21, 2023 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.