Jelly, 13 bytes
æḟ©3Ḥạ++_®)»ċ
A monadic Link that accepts a non-negative integer, \$N\$, and yields the value of \$f(n)\$.
Try it online! Or see the test-suite.
How?
Outputs the number of terms less than or equal to \$N\$ in another sequence - the unique, strictly increasing sequence of non-negative integers, \$a\$, such that \$a(a(n)) = 3n\$. This is A003605, which gives the indices at which an increase occurs in \$f\$.
æḟ©3Ḥạ++_®)»ċ - Link: non-negative integer, N
) - for each {I in [1..N] ([] if N==0)}:
æḟ 3 - floor to the nearest power of three
© - (and copy this to the register)
Ḥ - double {that}
ạ - {that} absolute difference {I}
+ - do this twice:
+ - add {I}
_® - subtract the value stored in the register
» - maximum with {N} (vectorises)
ċ - count occurrences of {N}
Jonathan Allan
- 115.4k
- 8
- 68
- 293