This challenge needed a convolution-based approach.
@(n)sum(n>conv((1:n).^4,[1 1]/2))^4
Try it online!
Explanation
The expression (1:n).^4
produces the row vector [1 16 81 256 ... n^4]
.
This vector is then convolved with [1 1]/2
, which is equivalent to computing the sliding average of blocks of size 2
. This implicitly assumes that the vector is left- and right-padded with 0
. So the first value in the result is 0.5
(average of an implicit 0
and 1
), the second is 8.5
(average of 1
and 16
), etc.
As an example, for n = 9
the result of conv((1:n).^4,[1 1]/2)
is
0.5 8.5 48.5 168.5 440.5 960.5 1848.5 3248.5 5328.5 3280.5
The comparison n>...
then yields
1 1 0 0 0 0 0 0 0 0 0
and applying sum(...)
gives 2
. This means that n
exceeds exactly 2
of the mid-points betwen biquadratic numbers (including the additional mid-point 0.5
). Finally, ^4
raises this to 4
to yield the result, 16
.
n^4
andn
alternates in sign. \$\endgroup\$ – Martin Ender Jul 10 '17 at 9:432 x n²
numbers: 2, 8, 18, 32, 50, 72, 98, ... \$\endgroup\$ – sergiol Jul 10 '17 at 10:35