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 Answer

deleted 652 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Jelly, (削除) 13 10 (削除ここまで) 109 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¬b3ḢḂȧ)S3ḍS

Try it online! Try it online! Or see the test-suite test-suite.

b3ḢḂȧ%3¬b3ḢḂȧ)S3ḍ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
 ȧ - {Lthat} logical AND {RN}
 ¬ 3ḍ - logical NOTthree ofdivides {that}? (vectorises)
 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)\$.

Alternative 9

Try it online! Or see the test-suite .Also seems to be a little more efficient, I'm not sure why.

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\$.Try it online!

æḟ©3Ḥạ++_®)»ċbḢ’+ọðƇ3L - Link: non-negative integer, N
 )Ƈ - forkeep eachthose {Ii inof [1..N] ([] iffor N==0)}which:
æḟ 3  ð 3 - floor to the nearest power off(i, three3):
 © b - (and copy thisconvert {i} to thebase register){3}
   - -  head doubleof {that} -> first trit (2 or 1)
   - {that} absolute differencedecrement {Ithat}
 +  -> L do(1 thisor twice:0)
 +  - add {Ii} _® - subtract the valueorder stored{3} in(repeated thedivisibility registercount)
 + - » - maximum{L} withadd {Nthat} (vectorises)
 ċL - count occurrences of {N}length

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.

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}

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.

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.

Try it online!

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
added 881 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

Jelly, 13 (削除) 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}

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}

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}
added 107 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293

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 monotonic, 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}

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 the unique monotonic sequence, \$a\$, such that \$a(a(n)) = 3n\$.

æḟ©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}

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}
deleted 101 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293
Loading
added 96 characters in body
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293
Loading
Source Link
Jonathan Allan
  • 115.4k
  • 8
  • 68
  • 293
Loading

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