Jelly, (削除) 13 10 (削除ここまで) 9 bytes
-3 bytes porting Dominic van Essen's implementation of my approach.
-1 byte thanks to Unrelated String (golfing the ¬ out by using a divisibility check.)
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
ȧ - {that} logical AND {N}
3ḍ - three divides {that}? (vectorises)
S - sum
Alternative 9
Also seems to be a little more efficient, I'm not sure why.
bḢ’+ọðƇ3L - Link: non-negative integer, N
Ƈ - keep those i of [1..N] for which:
ð 3 - f(i, 3):
b - convert {i} to base {3}
Ḣ - head of {that} -> first trit (2 or 1)
’ - decrement {that} -> L (1 or 0)
ọ - {i} order {3} (repeated divisibility count)
+ - {L} add {that}
L - length
Jonathan Allan
- 115.4k
- 8
- 68
- 293