16
\$\begingroup\$

From the infinite triangular array of positive integers, suppose we repeatedly select all numbers at Euclidean distance of \$\sqrt{3}\$, starting from 1:

$$ \underline{1} \\ \;2\; \quad \;3\; \\ \;4\; \quad \;\underline{5}\; \quad \;6\; \\ \;\underline{7}\; \quad \;8\; \quad \;9\; \quad \underline{10} \\ 11 \quad 12 \quad \underline{13} \quad 14 \quad 15 \\ 16 \quad \underline{17} \quad 18 \quad 19 \quad \underline{20} \quad 21 \\ \underline{22} \quad 23 \quad 24 \quad \underline{25} \quad 26 \quad 27 \quad \underline{28} \\ \cdots $$

Alternatively, you may think of it as "leave centers of a honeycomb pattern and cross out boundaries".

The resulting sequence (not yet on OEIS, unlike the polkadot numbers) is as follows:

1, 5, 7, 10, 13, 17, 20, 22, 25, 28, 31, 34, 38, 41, 44, 46, 49, 52, 55, 58, 61, 64,
68, 71, 74, 77, 79, 82, 85, 88, 91, 94, 97, 100, 103, 107, 110, 113, 116, 119,
121, 124, 127, 130, 133, 136, 139, 142, 145, 148, 151, 155, 158, 161, 164, 167, 170,
172, 175, 178, 181, 184, 187, 190, ...

The task is to output this sequence.

I/O rules apply. You can choose to implement one of the following:

  • Given the index \$n\$ (0- or 1-based), output the \$n\$th term of the sequence.
  • Given a positive integer \$n\$, output the first \$n\$ terms of the sequence.
  • Take no input and output the entire sequence by
    • printing infinitely or
    • returning a lazy list or a generator.

Standard rules apply. The shortest code in bytes wins.

\$\endgroup\$

12 Answers 12

10
\$\begingroup\$

Python, 34 bytes

Takes as input an integer \$ n \$, and outputs the \$ n \$-th term of the sequence (0-indexed).

lambda n:3*n-~((n*24+1)**.5%6%5<1)

Try it online!

Python 3, 43 bytes

Outputs the sequence forever.

n=1
while[print(n//8-~(n**.5%6%5<1))]:n+=24

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Besides, for an infinite output, this could work: n=0\n while 1:print(3*n-~((n*24+1)**.5%6%5<1));n+=1 which would be 49 bytes, if you consider '\n' to be 1 byte as an actual newline \$\endgroup\$ Commented Oct 19, 2022 at 4:23
  • \$\begingroup\$ Yes of course, and it can be taken down to 44 bytes by two more tricks: incrementing by 3 and placing the print in the condition. \$\endgroup\$ Commented Oct 19, 2022 at 4:28
4
\$\begingroup\$

Haskell, 45 bytes

-2 bytes thanks to @Grain Ghost.

[sum[0..n]+k|n<-[0..],k<-[0..n],mod(n+k)3==1]

Attempt This Online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ sum[1..n] is shorter than div(n^2+n)2. \$\endgroup\$
    – Wheat Wizard
    Commented Oct 19, 2022 at 1:26
3
\$\begingroup\$

JavaScript (V8), 55 bytes

Prints the sequence forever.

for(n=i=1;;i++)for(j=0;j<=i*3;n+=3+!j-(j++==i))print(n)

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 24 17 bytes

I…⌕A⭆⊕θ⭆⊕ι﹪⁺ιλ³1N

Try it online! Link is to verbose version of code. Outputs the first n terms. Explanation: Now a port of @JonathanAllan's Jelly answer, but taking some inspiration from @Steffan's Vyxal answer to save a byte.

      θ             Input `n`
     ⊕              Incremented
    ⭆               Map over implicit range and join
         ι          Current value
        ⊕           Incremented
       ⭆            Map over implicit range and join
            ι       Outer value
           ⁺        Plus
             λ      Inner value
          ﹪         Modulo
              ³     Literal integer 3
  ⌕A                Find all indices of
               1    Literal string `1`
 …                  Truncated to length
                N   Input `n` as an integer
I                   Cast to string
                    Implicitly print
\$\endgroup\$
1
  • \$\begingroup\$ Another way of looking at it is that the 2s appear in runs of n starting at the nth pentagonal number, so 0 2s at 0, 1 2 at 1, 2 2s at 5, 3 2s at 12, 4 2s at 22, 5 2s at 35 etc. \$\endgroup\$
    – Neil
    Commented Oct 19, 2022 at 8:53
2
\$\begingroup\$

Jelly, 9 bytes

ŻrḤ$3ḍFTḣ

A monadic Link that accepts a non-negative integer, \$n\$, and yields the first \$n\$ triangular honeycomb numbers.

Try it online!

How?

The triangle's \$i^{\text{th}}\$ row contributes every third element starting with the \$(3 - (i+1\pmod 3))^{\text{th}}\$ element...

ŻrḤ$3ḍFTḣ - Link: integer, n    e.g. 4
Ż         - zero-range (n)           [ 0,  1,    2,      3,        4]
   $      - last two links as a monad:
  Ḥ       -   double                 [ 0,  2,    4,      6,        8]
 r        -   inclusive range        [[0],[1,2],[2,3,4],[3,4,5,6],[4,5,6,7,8]]
    3     - three                    3
     ḍ    - divides?                 [[1],[0,0],[0,1,0],[1,0,0,1],[0,0,1,0,0]
      F   - flatten                  [ 1,  0,0,  0,1,0,  1,0,0,1,  0,0,1,0,0]
       T  - truthy indices           [ 1,          5,    7,   10,     13    ]
        ḣ - head (to index n)        [1,5,7,10]
\$\endgroup\$
2
\$\begingroup\$

Ruby, 35 30 29 bytes

-5 bytes thanks to Sʨɠɠan
-1 byte thanks to G B

Returns nth item of the sequence. Same technique as Python and Vyxal answers; give them upvotes.

->n{3*n-~33[(n*24+1)**0.5%6]}

Attempt This Online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 30 bytes: ->n{3*n-~1[(n*24+1)**0.5%6%5]} \$\endgroup\$
    – naffetS
    Commented Oct 19, 2022 at 3:26
  • \$\begingroup\$ @Sʨɠɠan Very clever. Thanks! \$\endgroup\$
    – Jordan
    Commented Oct 19, 2022 at 13:59
  • 1
    \$\begingroup\$ 29: ->n{3*n-~33[(n*24+1)**0.5%6]} \$\endgroup\$
    – G B
    Commented Oct 19, 2022 at 14:13
  • \$\begingroup\$ @GB Clever as well. Thanks! \$\endgroup\$
    – Jordan
    Commented Oct 19, 2022 at 15:52
2
\$\begingroup\$

Vyxal, 10 bytes

Þ::ʀ+3ḊfT›

Try it Online!

Outputs the infinite sequence.

-6 bytes (compared to my previous answer below) thanks to porting @Jonathan Allan's Jelly answer, so make sure to upvote that!

Þ::ʀ+3ḊfT›
Þ:         # Push an infinite list of non-negative integers
  :        # Duplicate
   ʀ+      # For each n in this list, add n to each item in a range [0..n]. This produces an infinite list like [[0], [1, 2], [2, 3, 4], [3, 4, 5, 6], ...]
     3Ḋ    # For each inner item, is it divisible by three?
       f   # Flatten
        T  # Get truthy (0-based) indices
         › # Increment

Previously:

Vyxal, 16 bytes

Þ∞ƛʀ+'ǒ1=;nɽ∑+;f

Try it Online!

Outputs the infinite sequence.

Þ∞ƛʀ+'ǒ1=;nɽ∑+;f
Þ∞ƛ               # Map n over positive integers:
   ʀ+             #  For each in [0..n], add n
     '            #  Filter for:
      ǒ           #   Modulo 3
       1=         #   Equals one?
         ;        #  Close filter
          nɽ∑+    #  For each, add the sum of [0..n-1]
              ;   # Close map
               f  # Flatten

Vyxal, 12 bytes

24*›√6%₅$T+›

Try it Online!

Outputs the \$n\$th element of the sequence. Port of @dingledooper's answer, so uvpote that!

24*›√6%₅$T+›
24*          # Multiply the input by 24
   ›         # Increment
    √        # Square root
     6%      # Modulo 6
       ₅     # Is it divisible by 5? (Returns 1 or 0)
        $T+  # Add input * 3
           › # Increment
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Wow, that FizzBuzz builtin actually came in useful for you? \$\endgroup\$
    – Neil
    Commented Oct 19, 2022 at 16:02
2
\$\begingroup\$

05AB1E, 12 11 bytes

Ports of @Sʨɠɠan's Vyxal answers, where the first is a port of @JonathanAllan's Jelly and the second is a port of @dingledooper's Python answer, so make sure to upvote them as well!

Given no input, it'll output the infinite sequence as list (11 bytes):

∞<DÝ+˜3Öƶ0K

Try it online.

Given \$n\$, it'll output the 0-based \$n^{th}\$ term (12 bytes)

Ð$24*>t6%5ÖO

Try it online or verify the first 25 items.

Explanation:

∞             # Push an infinite list of positive integers: [1,2,3,...]
 <            # Decrease each by 1 to make it non-negative: [0,1,2,...]
  D           # Duplicate this infinite list
   Ý          # Map each to a [0,val]-ranged list in the copy
    +         # Add the values of the lists at the same positions together
     ˜        # Flatten this list of lists
      3Ö      # Check for each integer whether it's divisible by 3
        ƶ     # Multiply each check by its 1-based index
         0K   # And then remove all 0s (the falsey checks)
              # (after which the infinite list is output implicitly as result)
Ð             # Triplicate the (implicit) input-integer
 $            # Push 1 and the input-integer yet again
  24*         # Multiply the top input by 24
     >        # Increase it by 1
      t       # Take the square-root of that
       6%     # Modulo-6
         5Ö   # Check if that is divisible by 5
           O  # And then sum all five values on the stack together:
              #  input + input + input + 1 + (sqrt(input*24)%6%5==0)
              # (which is output implicitly as result)
\$\endgroup\$
1
\$\begingroup\$

MathGolf, 12 bytes

M*)√6%5%┬ΓΣ)

Port of @dingledooper's Python answer.
Given \$n\$, it'll output the 0-based \$n^{th}\$ term.

Try it online.

Unfortunately ÷ (is divisible by builtin) is incorrectly implemented and doesn't support a float argument, otherwise the 5%┬ could have been for -1 byte.

Explanation:

M*            # Multiply the (implicit) input-integer by 24
  )           # Increase it by 1
   √          # Take the square-root of that
    6%        # Modulo-6
      5%┬     # Check if it's divisible by 5:
      5%      #  Modulo-5
        ┬     #  Check if it's equal to 0.0
         Γ    # Wrap the top four values into a list
              # (which uses the implicit input three times)
          Σ   # Sum this list together
           )  # Increase it by 1
              # (after which the entire stack is output implicitly as result)
\$\endgroup\$
1
\$\begingroup\$

Nibbles, 8.5 bytes (17 nibbles)

`?+.,~.+$,$%$3 2

Somewhat modified port of Jonathan Allan's Jelly answer: upvote that.
Outputs the infinite sequence.

`?+.,~.+$,$%$3 2
    ,~              # 1..infinity
   .                # map over each n
         ,$         #   1..n
       +$           #   add n 
      .    %$3      #   each modulo 3
  +                 # now flatten this list-of-lists
`?                  # and get indices of
               2    # all elments equal to 2

enter image description here

\$\endgroup\$
0
\$\begingroup\$

C (clang), 59 bytes

j,i;main(x){for(;i%3||printf("%d ",x);i=++i>j*2?++j:i)++x;}

Try it online!

Full program.

Iterate x=[1..] combined with growing series i.. of lines of the pyramid where starting value j increases by one.
When i%3 == 0 prints x

0 12 234 3456 45678 56789..
1 ..  5..7.10..13,  17, 20
\$\endgroup\$
0
\$\begingroup\$

Haskell, 43 bytes

[y-x+sum[1..x]|x<-[1..],y<-[1,4..x*2],y>=x]

Try it online!

Using similar approach of my C answer

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.