Generate n-ary numbers

Jelly, 12 bytes


Takes n and k (one-indexed) as command-line arguments.

Try it online!

How it works

1Ç#Ṫ     Main link. Left argument: n. Right argument: k

1        Set the return value to 1.
 Ç#      Execute the helper link above for r = 1, 2, 3, ... until k of them return
         a truthy value. Yield the list of all k matches.
   Ṫ     Tail; extract the last match.

Æf*³<‘Ạ  Helper link. Argument: r

Æf       Compute all prime factors of r.
  *³     Elevate them to the n-th power.
    <‘   Compare all powers with r + 1.
      Ạ  All; return 1 if all comparisons were true, 0 if one or more were not.

JavaScript (ES7), 95 90 bytes

Reasonably fast but sadly limited by the maximum number of recursions.


How it works

Rather than factoring an integer i and verifying that all its prime factors are less than or equal to x = floor(i1/n), we try to validate the latter assumption directly. That's the purpose of the inner function F():

F = (i, d) =>         // given an integer i and a divisor d:
  i - 1 ?             //   if the initial integer is not yet fully factored
    d > 1 ?           //     if d is still a valid candidate
      i % d ?         //       if d is not a divisor of i
        F(i, d - 1)   //         try again with d-1 
      :               //       else
        F(i / d, x)   //         try to factor i/d
    :                 //     else
      1               //       failed: yield 1
  :                   //   else
    --k               //     success: decrement k

We check if any integer d in [2 ... i1/n] divides i. If not, the assumption is not valid and we return 1. If yes, we recursively repeat the process on i = i / d until it fails or the initial integer is fully factored (i == 1), in which case we decrement k. In turn, the outer function f() is called recursively until k == 0.

Note: Due to floating point rounding errors such as 125**(1/3) == 4.9999…, the actual computed value for x is floor((i+1)1/n).


(Here with a 97-byte ES6 version for a better compatibility.)


console.log(f(17, 3)); // 144
console.log(f(13, 4)); // 243
console.log(f(4, 10)); // 4096

Brachylog, 21 bytes


Try it online!

This answer is one-indexed.


Input: [N:K]

:[1]cy              Retrieve the first K valid outputs of the predicate below with N as input
      t             Output is the last one

,1                  Output = 1
  |                 Or
   ,.$ph            Take the biggest prime factor of the Output
        :?^<=       Its Nth power is less than the Output