24
\$\begingroup\$

From the infinite triangular array of positive integers, suppose we select every 2nd numbers on every 2nd row as shown below:

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

The resulting sequence (A185868) is as follows:

1, 4, 6, 11, 13, 15, 22, 24, 26, 28, 37, 39, 41, 43, 45, 56, 58, 60, 62, 64, 66,
79, 81, 83, 85, 87, 89, 91, 106, 108, 110, 112, 114, 116, 118, 120, 137, ...

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\$
3
  • \$\begingroup\$ The phrasing of the first sentence makes it seem like you are given the triangular array as an input. \$\endgroup\$ Commented Oct 17, 2022 at 0:23
  • 1
    \$\begingroup\$ @97.100.97.109 Does it look better now? \$\endgroup\$
    – Bubbler
    Commented Oct 17, 2022 at 0:37
  • \$\begingroup\$ I think so, though you could be even more clear by phrasing it as something besides a command, e.g. "Imagine taking every second number..." or "Suppose we took every second number..." \$\endgroup\$ Commented Oct 17, 2022 at 0:40

22 Answers 22

13
\$\begingroup\$

Python, 35 bytes (@att)

lambda n:int((2*n)**.5-.5)**2+2*n-1

Attempt This Online!

Python, 39 bytes

lambda n:(int((8*n)**.5-1)//2)**2+2*n-1

Attempt This Online!

Given the 1-based index computes a single value.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ -4 bytes \$\endgroup\$
    – att
    Commented Oct 17, 2022 at 5:41
10
\$\begingroup\$

Python 2, 47 bytes

Outputs the sequence indefinitely.

n=x=1
while 1:exec'2;print x;x+='*n+'n-~n';n+=1

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Haskell, 34 bytes

[2*n^2-3*n+2*k|n<-[1..],k<-[1..n]]

Attempt This Online!

\$\endgroup\$
5
\$\begingroup\$

Jelly,  9  8 bytes

Ḷ²’xRĖḄḣ

A monadic Link that accepts a positive integer, \$n\$, and yields a list of the first \$n\$ odd-odd triangular polka dot numbers.

Try it online!

How?

Calculates the first \$T(n)=\frac{n(n+1)}{2}\$ terms, and outputs the first \$n\$.

Ḷ²’xRĖḄḣ - Link: integer, n     e.g. 5
Ḷ        - lowered range (n)         [    0,    1,    2,    3,    4]
 ²       - square (vectorises)       [    0,    1,    4,    9,   16]
  ’      - decrement                 [   -1,    0,    3,    8,   15]
    R    - range (n)                 [    1,    2,    3,    4,    5]
   x     - repeat elements           [   -1,    0,    0,    3,    3,    3,    8,    8,    8,     8,     15,     15,     15,     15,     15]
    Ė    - enumerate                 [[1,-1],[2,0],[3,0],[4,3],[5,3],[6,3],[7,8],[8,8],[9,8],[10,8],[11,15],[12,15],[13,15],[14,15],[15,15]]
     Ḅ   - convert from base 2 (vectorises)
           ...i.e. [a,b] -> 2a+b     [    1,    4,    6,   11,   13,   15,   22,   24,   26,    28,     37,     39,     41,     43,     45]
       ḣ - head (to index n)         [    1,    4,    6,   11,   13]
\$\endgroup\$
4
\$\begingroup\$

Rust, 43 bytes

|n|((n+n).sqrt().round()-1.).powi(2)+n+n-1.

Try it online!

A slightly modified version of loopy walt's answer.

\$\endgroup\$
4
\$\begingroup\$

Vyxal, 10 bytes

Þ∞:ẇ2Ḟ2vḞf

Try it Online! Outputs (theoretically) infinitely but crashes due to hitting the recursion limit caused by a bug.

Þ∞         # All positive integers
   ẇ       # Cut into slices of lengths...
Þ∞:        # Positive integers
    2Ḟ     # Get every second item
      2vḞ  # Get every second item of each
         f # Flatten the final array
\$\endgroup\$
3
  • 1
    \$\begingroup\$ ṁĊ2Ċ2CNN Husk, 8 with the exact same process \$\endgroup\$
    – Razetime
    Commented Oct 17, 2022 at 2:37
  • \$\begingroup\$ @Razetime maybe post it, theres no husk solution yet :P \$\endgroup\$ Commented Oct 17, 2022 at 2:38
  • \$\begingroup\$ This actually doesn't output infinitely - it stops after a bit due to max recursion depth (bug). \$\endgroup\$
    – naffetS
    Commented Oct 19, 2022 at 2:48
3
\$\begingroup\$

JavaScript (V8), 40 bytes

Prints the sequence forever.

for(n=i=q=1;;n+=--i?2:q+(i=++q))print(n)

Try it online!

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

Factor + math.unicode, 34 bytes

[ 2 * dup √ .5 - 1 /i sq + 1 - ]

Try it online!

Outputs the 1-indexed nth term of the sequence. Port of loopy walt's and att's Python answer.

word  | data stack              | comment
------+-------------------------+-----------------
      | 5                       | example input
2     | 5 2                     |
*     | 10                      |
dup   | 10 10                   |
√     | 10 3.16227766016838     | square root
.5    | 10 3.16227766016838 0.5 |
-     | 10 2.66227766016838     |
1     | 10 2.66227766016838 1   |
/i    | 10 2                    | integer divide
sq    | 10 4                    | square
+     | 14                      |
1     | 14 1                    |
-     | 13                      | output
\$\endgroup\$
3
\$\begingroup\$

sclin, 32 bytes

; ; tk2* ;2^2* ;3* - + flat
$W1+

Try it here! Returns an infinite list. This code is so weird...

For testing purposes (use -i flag when running locally):

; 50tk >A
; ; tk2* ;2^2* ;3* - + flat
$W1+

Explanation

Prettified code:

; ; tk 2* ; 2^ 2* ; 3* - + flat
$W 1+

; means "evaluate the next line as a function."

  • $W 1+ [1, ∞) as n
  • ; ; tk 2* [[2][2 4][2 4 6][2 4 6 8]...] as k*2
    • this works because tk ("take") vectorizes the second argument
  • ; 2^ 2* ; 3* - + n^2*2 - n*3 + k*2
    • this whole segment is vectorized
  • flat flatten

It's vectorizations all the way down!


sclin, 19 bytes

2*""Q.5^.5- I2^ +1-

Try it here! Port of @loopywalt/@att's answer. Takes a 1-based index, but the whole function vectorizes quite neatly too.

For testing purposes (use -i flag when running locally):

$W1+ ; 50tk >A
2*""Q.5^.5- I2^ +1-
\$\endgroup\$
2
\$\begingroup\$

R, 55 bytes

function(n,`?`=rbind)diffinv(rep(1:n*2+1?2,1?1:n))[n]+1

Try it online!

Returns the 1-based nth term.

\$\endgroup\$
2
  • \$\begingroup\$ or 37 bytes... \$\endgroup\$ Commented Oct 17, 2022 at 16:15
  • 1
    \$\begingroup\$ @DominicvanEssen you can post that...Also, it's vectorized; no sapply required! \$\endgroup\$
    – Giuseppe
    Commented Oct 17, 2022 at 17:02
2
\$\begingroup\$

Raku, 31 bytes

[\+] flat (1,3...*)Z(2 Xxx^∞)

Try it online!

This is an expression for the lazy infinite sequence of numbers.

  • (1, 3 ... *) is the infinite sequence of odd numbers.
  • xx is the replication operator, which replicates its left-hand side a number of times given by its right-hand side.
  • X is the cross-product metaoperator. Applied to the replication operator xx, it makes a new operator that replicates as a cross product.
  • ^∞ is the unbounded range of increasing integers starting at zero.
  • 2 Xxx ^∞ produces a list of 2 replicated each number of times from zero to infinity: (), (2,), (2, 2), (2, 2, 2), ....
  • Z zips the odd numbers with the list of increasing numbers of twos: (1, ()), (3, (2,)), (5, (2, 2)), (7, (2, 2, 2)), ....
  • flat flattens that list: 1, 3, 2, 5, 2, 2, 7, 2, 2, 2, .... That gives the list of differences between adjacent terms of the sequence.
  • [\+] gives the list of partial sums of the previous sequence, which is the desired polka-dot sequence.
\$\endgroup\$
2
\$\begingroup\$

R, 37 bytes

function(x)(((8*x)^.5-1)%/%2)^2+2*x-1

Try it online!

An alternative to Giuseppe's R answer, here using a variant of loopy walt's approach.

\$\endgroup\$
2
\$\begingroup\$

Excel, 27 bytes

=INT((2*A1)^.5-.5)^2+2*A1-1

Direct implementation of loopy walt's answer in Python except it's shorter in Excel. Given the 1-based index computes a single value. Input is in A1. When copying the above into Excel, it will automatically add leading zeroes to change .5 to 0.5.

Screenshot


This 41 byte solution prints the first 1,048,576 terms (or however many rows you have in your version of Excel):

=LET(r,ROW(A:A),INT((2*r)^.5-.5)^2+2*r-1)
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 12 bytes

Þ∞:ɾ•y_ƛy$;f

Try it Online!

Outputs the sequence forever.

Explained

Þ∞:ɾ•y_ƛy$;f
Þ∞:          # Push two copies of an infinite list of positive integers
   ɾ         # In the second copy of the infinite list, push range [1, n] for each item
    •        # And mold the first copy of the infinite list to the shape of the second
     y_      # uninterleave the infinite list, giving two lists: the list of every second row starting at row 0, and the list of every second row starting at row 1. Discard the list starting at row 1.
       ƛy$;f # To each row in the remaining list, uninterleave the row, and return the list of every second item starting at index 0. Then flatten the result.
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 22 bytes

NθFθF⊕ι⊞υ×ιιIE…υθ⊕⁺ι⊗κ

Try it online! Link is to verbose version of code. Outputs the first n terms of the sequence. Explanation: Port of @JonathanAllan's Jelly answer.

Nθ

Input n.

FθF⊕ι⊞υ×ιι

Create an array containing each of the squares 0, 1, 4, 9, ... , ... each repeated i+1 times.

IE…υθ⊕⁺ι⊗κ

Take the first n elements of the array, and add to each the incremented doubled index (0-indexed).

20 bytes using the newer version of Charcoal on ATO:

NθIE…ΣEθE⊕ι×ιιθ⊕⁺ι⊗κ

Attempt This Online! Link is to verbose version of code. Outputs the first n terms of the sequence. Explanation:

Nθ                      Input `n` as a number
       θ                Input `n`
      E                 Map over implicit range
          ι             Current value
         ⊕              Incremented
        E               Map over implicit range
            ι           Outer value
           ×            Multiplied by
             ι          Outer value
     Σ                  Concatenated
    …                   Truncated to length
              θ         Input `n`
   E                    Map over squares
                 ι      Current square
                ⁺       Plus
                   κ    Current index
                  ⊗     Doubled
               ⊕        Incremented
  I                     Cast to string
                        Implicitly print
\$\endgroup\$
1
\$\begingroup\$

Desmos, 31 bytes

f(n)=floor(\sqrt{2n}-.5)^2+2n-1

Literally just a port of loopy walt's python answer, go upvote that answer too!

Try It On Desmos!

Try It On Desmos! - Prettified

\$\endgroup\$
3
  • \$\begingroup\$ f(n)=floor((2n)^.5-.5)^2+2n-1 saves 2 bytes. \$\endgroup\$
    – naffetS
    Commented Oct 18, 2022 at 0:41
  • \$\begingroup\$ @Sʨɠɠan Did you try actually pasting that in? It doesn't work. f(n)=floor((2n)^{.5}-.5)^2+2n-1 works but it's the same byte count as what I have (trust me, I checked before I posted this answer). \$\endgroup\$
    – Aiden Chow
    Commented Oct 18, 2022 at 6:47
  • \$\begingroup\$ Oh lol, forgot to turn off the extension. \$\endgroup\$
    – naffetS
    Commented Oct 18, 2022 at 12:35
1
\$\begingroup\$

05AB1E, 9 8 bytes

-1 byte by porting @corvus_192's Rust answer, which is a slightly modified version of @loopyWalt's Python answer, so make sure to upvote them as well!

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

·Dtò<n+<

Try it online or verify the first 25 items.

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

∞£ιнειн}˜

Try it online.

Explanation:

·         # Double the (implicit) input-integer
 D        # Duplicate it
  t       # Take the square-root of the copy
   ò      # Round it to the nearest integer
    <     # Decrease it by 1
     n    # Square it
      +   # Add it to the doubled input that's still on the stack
       <  # Decrease it by 1
          # (after which it is output implicitly as result)
∞         # Push a positive infinite list: [1,2,3,4,5,...]
 £        # Split it into parts of itself (since there is no input):
          #  [[1],[2,3],[4,5,6],[7,8,9,10],[11,12,13,14,15],...]
  ι       # Uninterleave it into two parts
   н      # Pop and only keep the first
    ε  }  # Map over each remaining inner list:
     ιн   #  Do the same for those
        ˜ # Flatten the list of lists
          # (after which the infinite list is output implicitly as result)
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 8 bytes

d:√ṙ‹²+‹

Try it Online!

Port of 05AB1E, which is a port of Rust, so make sure to upvote those answers!

\$\endgroup\$
1
\$\begingroup\$

C (GCC), 68 65 bytes

-2 bytes thanks to @Sʨɠɠan

i;r;main(s){for(;i<=r||(i=0,s+=++r+ ++r);i+=2)printf("%d ",s+i);}

Attempt This Online!

\$\endgroup\$
2
  • \$\begingroup\$ )|( can be , \$\endgroup\$
    – naffetS
    Commented Oct 19, 2022 at 0:45
  • 1
    \$\begingroup\$ 63: i;r;main(s){for(;i>r?i=0,s+=++r+ ++r:1;i+=2)printf("%d ",s+i);} \$\endgroup\$
    – naffetS
    Commented Oct 20, 2022 at 0:06
1
\$\begingroup\$

Pip, 24 bytes

((RTDBa-0.5)//1)E2+DBa-1

Try It Online!

Port of loopy walt's Python answer, so go upvote his answer too!

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

><>, 39 bytes

Prints the infinite sequence.

130>$:2+@$:1+@v
2$-1v!?:$oan:{<}+
~+}v>

Try it online!

><>, 38 bytes

Prints the nth term 1-indexed.

:8*$2*1v
}(?!v1+>::*{:
+1-n>2-2,:1%-:*

Try it online!

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

Pyt, 10 bytes

2*Đ√½-Ɩ²+⁻

Try it online!

              implicit input
2*            double
  Đ           duplicate on the stack
   √          take the square root of top
    ½-        subtract 1/2
      Ɩ²      cast to an integer and square
        +     add the stack
         ⁻    decrement
              implicit output

Port of loopy walt's Python answer

If floating-point returns are allowed, then 9 bytes is possible:

2*Đ√⎶⁻²+⁻

Try it online!

Port of corvus_192's Rust 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.