Skip to main content
2 of 3
fixed the byte count / added an alternate version
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

#JavaScript (ES7), 64 bytes

A recursive function taking input in currying syntax (n)(p). Returns a space-separated list of pairs of integers, or an empty string if no solution exists. Uses the same algorithm as user202729's Python answer.

n=>p=>(g=x=>x--?((y=(x**p+n)**(1/p))%1?[]:[y,x]+' ')+g(x):[])(n)

Or 60 bytes with 0-terminated, encapsulated arrays:

n=>p=>(g=x=>x--&&((y=(x**p+n)**(1/p))%1?g(x):[y,x,g(x)]))(n)

This would output [ 9, 7, [ 6, 2, 0 ] ] for f(32)(2).

###Test cases

let f =

n=>p=>(g=x=>x--?((y=(x**p+n)**(1/p))%1?[]:[y,x]+' ')+g(x):[])(n)

console.log(f(50)(2))
console.log(f(32)(2))
console.log(f(7)(3))
console.log(f(665)(6))

Arnauld
  • 197.6k
  • 20
  • 179
  • 650