Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

5 of 6
added 881 characters in body
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Jelly, (削除) 13 (削除ここまで) 10 bytes

-3 bytes porting Dominic van Essen's implementation of my approach.

b3ḢḂȧ%3¬)S

Try it online! Or see the test-suite.

How?

b3ḢḂȧ%3¬)S - Link: non-negative integer, N
 ) - for each {I in [1..N] ([] if N==0)}:
b3 - convert {I} to base three
 Ḣ - head of {that}
 Ḃ - {that} mod two -> L
 %3 - {I} mod three -> R
 ȧ - {L} logical AND {R}
 ¬ - logical NOT of {that}
 S - sum

(I sometimes wish there was a logical NAND dyad, or at least one that acts that way with zeros and ones.)


Original at 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

AltStyle によって変換されたページ (->オリジナル) /