Each natural number (including 0) can be written as a sum of distinct powers of integers (with a minimum exponent of 2). Your task is to output the smallest power required to represent \$n\$.
For example:
2 = 1^2 + 1^3 // Output 3
9 = 1^2 + 2^3 = 3^2 // Output 2
15 = 2^2 + 2^3 + 1^4 + 1^5 + 1^6 // Output 6
20 = 2^2 + 0^3 + 2^4 // Output 4
Ungolfed slow reference implementation (js):
function f(x){
for (let maxPower = 2; true; maxPower++){
let bases = Array(maxPower - 1).fill(0);
let sum = 0;
while (sum != x && bases[0] ** 2 <= x){
sum = bases.reduce((a, c, i) => a + c ** (i + 2), 0);
let nextIncr = bases.length - 1;
do {
if (nextIncr < bases.length - 1) bases[nextIncr + 1] = 0;
bases[nextIncr]++;
} while (nextIncr && bases[nextIncr] ** (nextIncr--) > x)
}
if (sum == x) return maxPower;
}
}
This is a sequence challenge. The sequence is naturally 0-indexed, so you may not 1-index it. You may...
- Take a positive integer \$n\$ and output the first \$n\$ values.
- Take a positive integer \$n\$ and output the value for \$n\$.
- Take no input and output values indefinitely.
The sequence starts: 2,2,3,4,2,3,4,5,3,2,3,4,3,4,5,6,2,3,4,5
This is code-golf, so the shortest code in bytes wins.
--
Honestly, I'm surprised this isn't on the OEIS. I'm also uncertain whether there's any number above 6 in the sequence - not that I've searched that far.
1
each time? Or do they just have to be distinct? \$\endgroup\$0^x
for all of the missing ones. \$\endgroup\$