Skip to main content
1 of 2
Neil
  • 184.4k
  • 12
  • 76
  • 290

Charcoal, 30 bytes

NθFθFθ⊞υX⊕ι⁺²κI⟦⌈Φυ¬›ιθ⌊Φυ¬‹ιθ 

Try it online! Link is to verbose version of code. Explanation: Port of @Guiseppe's R answer.

Nθ 

Input n.

FθFθ⊞υX⊕ι⁺²κ 

Generate a list of perfect powers from to nⁿ⁺¹.

I⟦⌈Φυ¬›ιθ⌊Φυ¬‹ιθ 

Find the largest one not greater than n and the smallest one not less than n.

42 bytes for a less inefficient version:

NθFE⊕₂θX⊕ι…·²⎇ιL↨θ⊕ι²Fι⊞υκI⟦⌈Φυ¬›ιθ⌊Φυ¬‹ιθ 

Try it online! Link is to verbose version of code. Explanation: As above but only loops up to ⌊√n⌋ and only powers up to 1+⌊logᵢn⌋ (or 2 if i=1).

Neil
  • 184.4k
  • 12
  • 76
  • 290