[Question]
  [
Let's consider the sequence \$S\$ consisting of one \$1\$ and one \$0\$, followed by two \$1\$'s and two \$0\$'s, and so on:
$$1,0,1,1,0,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0,...$$
(This is [A118175](https://oeis.org/A118175): *Binary representation of n-th iteration of the Rule 220 elementary cellular automaton starting with a single black cell.*)
Given \$n>0\$, your task is to output \$a(n)\$, defined as the number of \$1\$'s among the \$T(n)\$ first terms of \$S\$, where \$T(n)\$ is the \$n\$-th [triangular number](https://oeis.org/A000217).
The first few terms are:
$$1,2,3,6,9,11,15,21,24,28,36,42,46,55,65,70,78,91,99,105,...$$
One way to think of it is to count the number of \$1\$'s up to the \$n\$-th row of a triangle filled with the values of \$S\$:
```
1 (1)
01 (2)
100 (3)
1110 (6)
00111 (9)
100001 (11)
1111000 (15)
00111111 (21)
000000111 (24)
1111000000 (28)
01111111100 (36)
...
```
## Rules
You may either:
* take \$n\$ as input and return the \$n\$-th term, 1-indexed
* take \$n\$ as input and return the \$n\$-th term, 0-indexed
* take \$n\$ as input and return the \$n\$ first terms
* take no input and print the sequence forever
This is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") challenge.
  
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 41 bytes
```
Sum[1-⌈s=√n⌉+Round@s,{n,#(#+1)/2}]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n277P7g0N9pQ91FPR7Hto45ZeY96OrWD8kvzUhyKdarzdJQ1lLUNNfWNamPV/gcUZeaVKOg7pOs7BCXmpadGGxnE/v8PAA "Wolfram Language (Mathematica) – Try It Online")
-2 bytes from @ZippyMagician
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 9 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
ḤR>)FŒHṪS
```
A monadic Link accepting \$n\$ which yields \$a(n)\$.
**[Try it online!](https://tio.run/##y0rNyan8///hjiVBdppuRyd5PNy5Kvj///@mAA "Jelly – Try It Online")** Or see the [test-suite](https://tio.run/##AR4A4f9qZWxsef//4bikUj4pRsWSSOG5qlP/MjDDh@KCrP8 "Jelly – Try It Online").
### How?
We can think of \$S\$ as being built in blocks of length \$2i\$ where each block is a string of \$i\$ ones followed by \$i\$ zeros: `10 1100 111000 ...`.
If we stop at \$i=x\$ and call the result \$S\_x\$ we know that \$S\_x\$ necessarily contains an equal number of ones and zeros.
We also know that the length of \$S\_x\$ will be \$\sum\_{i=1}^{x}2i = 2 \sum\_{i=1}^{x}i = 2T(x)\$.
So the value of \$a(x)\$ is the count of ones in the first half of \$S\_x\$.
An alternative way to get this same result is to subtract the count of the *zeros* in the first half of \$S\_x\$ from \$T(x)\$, and since \$S\_x\$ contains an equal number of ones and zeros this must also be the count of zeros in the second half of \$S\_x\$. Therefore we can form the complement of \$S\_x\$ and count the ones in its second half:
```
ḤR>)FŒHṪS - Link: integer, n
   )      - for each (i in [1..n]): e.g. i=3
Ḥ         -   double                       6
 R        -   range                        [1,2,3,4,5,6]
  >       -   greater than i?              [0,0,0,1,1,1]
    F     - flatten -> [0,1,0,0,1,1,0,0,0,1,1,1,...]
     ŒH   - split into two equal length parts
       Ṫ  - tail
        S - sum
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 8 bytes
```
Σ↑ṁṘḋ2NΣ
```
[Try it online!](https://tio.run/##AR0A4v9odXNr///Oo@KGkeG5geG5mOG4izJOzqP///8xMg "Husk – Try It Online") or [Verify first 12 values](https://tio.run/##ASQA2/9odXNr/23igoHhuKP/zqPihpHhuYHhuZjhuIsyTs6j////MTI "Husk – Try It Online")
Returns the \$n^{th}\$ value of the sequence, 1 indexed.
## Explanation
```
Σ↑ṁṘḋ2NΣ
  ṁ   N  map the following across natural numbers and concatenate
   Ṙḋ2   replicate [1,0] n times
 ↑       take all values
       Σ till the triangular number of the input
Σ        sum them
```
[Answer]
# [Haskell](https://www.haskell.org/), 48 bytes
```
f n=sum$sum[1..n]`take`do z<-[1..];[1,0]<*[1..z]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P00hz7a4NFcFiKMN9fTyYhNKErNTE1LyFapsdEEisdbRhjoGsTZaIE5V7P/cxMw824KizLwSldzEAoU0BZC4oUHsfwA "Haskell – Try It Online")
# [Haskell](https://www.haskell.org/), 48 bytes
```
sum.(take.sum.r<*>(([1,0]<*).r=<<).r)
r n=[1..n]
```
[Try it online!](https://tio.run/##FcgxDoAgEADB3ldQWIDRi/TgR5DiYkSJQAjo9z2l2Uz2xHrtIZDTK9UnAr/x2qGpqGHh3MhxtmoQULRSf0VXWNJGAiRLEX3Sufh09xEzc6x9OVt6NxfwqDRtOX8 "Haskell – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 47 bytes
```
f=lambda n,k=8:k>n*-~n*2or(-k**.5%2<1)+f(n,k+4)
```
[Try it online!](https://tio.run/##DctBDoMgEEbhfU/xb4gMShMmbWKMeheaijW0g0E33fTqlNVbvHz793wl4VLC9Pafx9NDujj1Q5zF2J8YTlnbaMz1rnh01AZdf3ujElKGYBNkL@uiXQd2NFyAPW9yolF8wM6oaaAqQpVE5Q8 "Python 2 – Try It Online")
**52 bytes**
```
lambda n:sum(-(k+1)**.5%1<.5for k in range(n*-~n/2))
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUSHPqrg0V0NXI1vbUFNLS89U1dBGzzQtv0ghWyEzT6EoMS89VSNPS7cuT99IU/M/SCIPIWGoo2BkqGnFpaBQUJSZV6KgrmpUrKBrpwCk1BVUNfJ0FNI08oDaAA "Python 2 – Try It Online")
Based on the formula for \$S\$ that [user's noted](https://codegolf.stackexchange.com/a/214621/20260) from the OEIS page of [A118175](https://oeis.org/A118175). We simplify it to the following, one-indexed using Booleans for 0/1:
$$ S(k) = \mathrm{frac}(-\sqrt{k}) < \frac{1}{2},$$
where \$\mathrm{frac}\$ takes the fractional part, that is the difference between the number and its floor. For example, \$\mathrm{frac}(-2.3)=0.7\$. This is equivalent to \$\sqrt{k}\$ being at most \$\frac{1}{2}\$ lower than its ceiling.
The code simply sums $$\sum\_{k=1}^{n(n+1)/2} S(k),$$ but shifting the argument \$k\$ by one to account for the zero-indexed Python range.
---
**57 bytes**
```
def f(n):N=n*-~n/2;a=round(N**.5);print(a+N-abs(a*a-N))/2
```
[Try it online!](https://tio.run/##HcyxDYMwEAXQnilOVHfGBmEpDRYZwTtcBCY038iQIk1Wd5QM8N7xvp4ZvtZlTZQYMsUZxn0w@KBzyS8sHI3pbxKOsuNi7aLTx8lq1EWRwdeUC4F2UFFsK4@W/ChTQ/QHBNu6e2vDL69f "Python 2 – Try It Online")
Outputs floats. A direct arithmetic formula. Thanks to Arnauld for -1 byte
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~12~~ 9 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
LxL@˜2äнO
```
-2 bytes by taking inspiration from [*@JonathanAllan*'s Jelly answer](https://codegolf.stackexchange.com/a/214616/52210) for generating the `[1,0,1,1,0,0,1,1,1,0,0,0,...]` list.
Outputs the \$n^{th}\$ value. (Thanks to *@ovs*.)
[Try it online](https://tio.run/##yy9OTMpM/f/fp8LH4fQco8NLLuz1///fEgA) or [verify the first 10 test cases](https://tio.run/##yy9OTMpM/R/i6mevpPCobZKCkr3ff58KH4fTc4wOL7mw1/@/zn8A).
**10 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage) version that outputs the infinite sequence instead:**
```
∞xL@˜∞£OηO
```
[Try it online.](https://tio.run/##yy9OTMpM/f//Uce8Ch@H03OA9KHF/ue2@///DwA)
**Explanation:**
```
L           # Push a list in the range [1, (implicit) input]
            #  i.e. input=5 → [1,2,3,4,5]
 x          # Double each value (without popping)
            #  [2,4,6,8,10]
  L         # Convert each value to a [1,n] list as well
            #  [[1,2],[1,2,3,4],[1,2,3,4,5,6],[1,2,3,4,5,6,7,8],[1,2,3,4,5,6,7,8,9,10]]
   @        # Check for each value in the [1,input] list that it's >= the values in the
            # inner ranged lists
            #  [[1,0],[1,1,0,0],[1,1,1,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,0,0,0,0,0]]
    ˜       # Flatten it
            #  [1,0,1,1,0,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,0]
     2ä     # Split it into 2 equal-sized parts
            #  [[1,0,1,1,0,0,1,1,1,0,0,0,1,1,1],[1,0,0,0,0,1,1,1,1,1,0,0,0,0,0]]
       н    # Only leave the first item
            #  [1,0,1,1,0,0,1,1,1,0,0,0,1,1,1]
        O   # And sum this list
            #  9
            # (after which this sum is output implicitly as result)
∞           # Push an infinite list of positive integers
            #  [1,2,3,...]
 xL@˜       # Same as above
            #  [1,0,1,1,0,0,1,1,1,0,0,0,...]
     ∞      # Push an infinite list again
      £     # Split the list into parts of that size
            #  [[1],[0,1],[1,0,0],[1,1,1,0],...]
       O    # Sum each inner list
            #  [1,1,1,3,...]
        η   # Take the prefixes of that list
            #  [[1],[1,1],[1,1,1],[1,1,1,3],...]
         O  # Sum each inner list
            #  [1,2,3,6,...]
            # (after which the infinite list is output implicitly)
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 11 bytes
```
⁵DxⱮRFḣRS$S
```
[Try it online!](https://tio.run/##ASQA2/9qZWxsef//4oG1RHjisa5SRuG4o1JTJFP/MTA7w4ck4oKsR/8 "Jelly – Try It Online")
Takes \$n\$, outputs \$a(n)\$, 1-indexed
## How it works
```
⁵DxⱮRFḣRS$S - Main link. Takes n on the left
⁵           - 10
 D          - [1, 0]
    R       - [1, 2, ..., n]
   Ɱ        - For each i in [1, 2, ..., n]:
  x         -   Repeat [1, 0] i times
     F      - Flatten the array
         $  - Group the previous two commands into a monad T(n):
       R    -   [1, 2, ..., n]
        S   -   Sum
      ḣ     - Take the first T(n) elements of the sequence
          S - Take the sum, essentially counting the 1s
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~24~~ 13 bytes
```
ＩΣ∕⭆⊕Ｎ⭆10×ιλ²
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM5sbhEI7g0V8MlsywzJVUjuAQonO6bWKDhmZdclJqbmleSmgJkF5SW@JXmJqUWaWhq6iggVCkZGijpKIRk5qYWa2TqKORogqSNgKSm9f//hgb/dctyAA "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
     Ｎ          Input `n`
   ⭆⊕           Map over inclusive range
      ⭆10       Map over characters of literal string `10`
           λ    Current character
         ×      Repeated by
          ι     Outer index
  ∕         ²   First half of resulting string
 Σ              Digital sum (i.e. count `1`s)
Ｉ               Cast to string
                Implicitly print
```
Previous 24-byte more Charoal-y solution:
```
ＮθＧLθψ¤⭆θ⭆²⭆⊕ιλ≔№ＫＡ0θ⎚Ｉθ
```
[Try it online!](https://tio.run/##TU5BCoMwELz7iuBpAxZsj/UkQkGwRegLUrvY0HWjMSn4@nQ9tXMYmBmYmeFl/OAMpdTyHMMtTg/0sOgq6x1to2M4d4VaCrWJdbFEcA/e8ng1M4j7E6d/0fLgcUIO@ASrC0VaUGX1utqRoXGRA/SI71r6JM7LXHgfbQiNh31dqgI0Zg1yRlcpHct0@NAX "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｎθ
```
Input `n`.
```
ＧLθψ
```
Draw an empty right triangle of side `n`.
```
¤⭆θ⭆²⭆⊕ιλ
```
Fill it using the string `010011000111...`. (The string is always twice as long as the triangle.) Charcoal's fill paints the provided string into the area to fill (see for example [Bake a slice of Pi](https://codegolf.stackexchange.com/q/93615/17602)). Note that the `0`s and `1`s are swapped.
```
≔№ＫＡ0θ
```
Get the number of `0`s that were actually printed.
```
⎚Ｉθ
```
Clear the canvas and print the result.
[Answer]
# [Python 2](https://docs.python.org/2/), 62 bytes
```
a=lambda n,k=1:-~n*n>k*k*2and k+a(n,k+1)or max(0,k-~n*n/2-k*k)
```
[Try it online!](https://tio.run/##HYo7DoMwEAX7nOI1CNvYCt4SCe6ykfNZOSzIoiBNru5YqUaamf1zvDalWnl@83pLDPV5jlP4qtMlu@yINSEPbFoYot0KVj7N6PN/uVJok62P5gWiKKzPu4keNNrpAuxF9EDfUUJY0NCjM@LBRqytPw "Python 2 – Try It Online")
This is based on
$$
\begin{align}
a(n) &= f(\frac{n\cdot(n+1)}{2}, 1) \\ \\
f(n, k) &= 
\begin{cases}
k+f(n-2k, k+1), & \text{if $n > k$} \\
\operatorname{max}(0, n), & \text{if $n \le k$}
\end{cases}
\end{align}
$$
but the conditions and the base case are more convoluted to get this into a single recursive function.
[Answer]
# [K (oK)](https://github.com/JohnEarnest/ok), ~~30~~ ~~25~~ 24 bytes
-6 bytes thanks to coltim
```
{+/(x+/!x)#,/x{0,x,1}\1}
```
[Try it online!](https://tio.run/##y9bNz/7/P82qWltfo0JbX7FCU1lHv6LaQKdCx7A2xrD2f5q6obaikcF/AA "K (oK) – Try It Online")
Returns the n-th term 1-indexed.
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~100~~ ~~89~~ ~~85~~ ~~84~~ 77 bytes
-11: Change `a**2` to `a*a` and simplify `1-Math.ceil(c)+Math.round(c)` to `Math.ceil(c)-c<0.5` ([@xnor](https://codegolf.stackexchange.com/users/20260/xnor))
-4: Move `c=Math.sqrt(b+1)` inside `Math.ceil(c)` and omit the `f=` ([@user](https://codegolf.stackexchange.com/users/95792/user))
-1: Change ...`c<0.5` to ...`c<.5` ([@xnor](https://codegolf.stackexchange.com/users/20260/xnor))
-7: Remove unnecessary `(` and `)`, and change `Math.sqrt(`...`)` to ...`**.5` ([@Samathingamajig](https://codegolf.stackexchange.com/users/98937/samathingamajig))
```
a=>(x=0,Array((a*a+a)/2).fill().map((a,b)=>x+=Math.ceil(c=(b+1)**.5)-c<.5),x)
```
[Try it online!](https://tio.run/##Dc1BDoIwEEDRvacgrGYoVNCwEUriAVx5gqEWGFMpKY3BGM9e2fzF2/wnvWnVnpdQzO5h4qAgkupgU2V@9Z4@AJSRIDyeUA5sLaB80bJr3qPqNqFuFCapDVvQCnpRYZbJGgvd7s03jNgcDoPzYE1IWJUNt1XdsBD41W5enTXSuhFYpJdUpEkqvVkMBTgXLIO7B8/zuD@tmccwoRiAEX9N/AM "JavaScript (Node.js) – Try It Online")
[Answer]
# APL+WIN, ~~26~~ 21 bytes.
minus 5 bytes thanks to Adam.
Prompts for integer:
```
+/(+/m)↑∊(m←⍳⎕)∘.⍴1 0
```
[Try it online! Thamks to Dyalog Classic](https://tio.run/##FcvNCcJAEMXx@1ThMSHE7Ev87EGLWCIRYYNCTjYQFyGiB8E@tKJpZH1z@M9lfs9fQnm4@nA@lm3ww3Bqkz7e@52Oz0Y03rpUVFlR9bmOL433rOdDpy9NrvEz1@mHmUuEkjqBdFKzhi3Ykq3Ymm3YlsHZMQmjMAvDMA3jMA8bwBa1kz8 "APL (Dyalog Classic) – Try It Online")
[Answer]
# [Python 3](https://docs.python.org/3/), 69 bytes
```
lambda n:sum([j for i in range(1,n+1)for j in[1]*i+i*[0]][:n*-~n//2])
```
[Try it online!](https://tio.run/##TcsxDoMwEAXRq2zpNURg6CzlJI4LI@JkEXyQIQUNVzfQpX2aWfbtO6PN8fnKY5i6PhDs@puUGyjOiYQElAI@b2VKFIZvHC50xmspRLvae2ehHweqqvGc7wD/l2nYLkmwqajAnE8 "Python 3 – Try It Online")
[Answer]
# Scala, ~~66 58~~ 51 bytes
```
n=>1 to n flatMap(i=>""*i+"\0"*i)take(n*n+n>>1)sum
```
[Try it online](https://scastie.scala-lang.org/PJ6Mt7irSY6rMkzg4dExaQ)
There is an unprintable character `0x01` inside the first quote.
An anonymous function that takes an integer `n` and returns the nth element of the sequence (1-indexed).
[Answer]
# [Haskell](https://www.haskell.org/), 46 bytes
```
f n=sum[1|a<-[1..n],b<-[1..a],a*a-b<n*(n+1)/2]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P00hz7a4NDfasCbRRjfaUE8vL1YnCcJKjNVJ1ErUTbLJ09LI0zbU1DeK/Z@bmJlnW1CUmVeioKKQm1igkKYAUmpiEPsfAA "Haskell – Try It Online")
---
**46 bytes**
```
f n=sum[max 0$min a$n*(n+1)/2-a*a+a|a<-[1..n]]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P00hz7a4NDc6N7FCwUAlNzNPIVElT0sjT9tQU99IN1ErUTuxJtFGN9pQTy8vNvZ/bmJmnm1BUWZeiYKKQm5igUKaAkjKxCD2PwA "Haskell – Try It Online")
---
**48 bytes**
```
f n=sum[1|k<-[2,4..n*n+n],even$ceiling$sqrt$2*k]
```
[Try it online!](https://tio.run/##BcExDsAQFADQq/zBpColxjqJGKShFfwo2ql31/cu15PPec4AqPtTjPjSvhrJFOdIcUHL/OuRHD7miCfpdxtE0mRncRF1bREHECiuQgAjOFebnT8 "Haskell – Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~84~~ ~~82~~ 61 bytes
Saved 2 bytes thanks to [ErikF](https://codegolf.stackexchange.com/users/79060/erikf)!!!
```
c;p;f(n){for(c=p=0,n=n*-~n/2;n>2*p;n-=2*p++)c+=p;c+=n<p?n:p;}
```
[Try it online!](https://tio.run/##VVDbbsIwDH3nK6xKSL24ok0v0IWwh2lfMXhAaTqqaSZqeaiGul/vXCgMIsVOjo@PfKzDT62HQUsrK5e8c3VsXK2sipAU@eEvLYSkjfCtpFBxCgJPB8pKDrS2r/RiZT/UdILvfU2uNzvPgM8ImM4afTLlxw4UnGMUmGCOBcYxxhkKBlIUK0xyTAWmOWYZ5hkuI1yusIixYGaU9fKipw/7xtcHo79Mw3qjoLPt3sW2K974Zg7C4z9xpj52A@44TE2l6bgtktNzDW39Y46VexvTW0yAf0ckBMGF7cHV1s0asdJVJoBYPpVaLo2LfEYNo/d9XDp3/wTbMKVynXkJ4QY4ztstsSNCaBFurlulzG6S7Wf98Ac "C (gcc) – Try It Online")
Inputs a \$1\$-based number \$n\$ and returns the \$n^{\text{th}}\$ term.
[Answer]
# [Haskell](https://www.haskell.org/), 50 bytes
```
r?x|q<-sum[0..x]-r*r,r>q=min q 0|l<-r+1=l+l?x
(0?)
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/v8i@oqbQRre4NDfaQE@vIla3SKtIp8iu0DY3M0@hUMGgJsdGt0jb0DZHO8e@4n9uYmaebUFRZl6JSm5igYaBvWa0oZ6ekUHsfwA "Haskell – Try It Online")
Slightly longer but complete different approach from the existing Haskell answer. This one is basically all arithmetic whereas the existing one builds the list from scratch.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `s`, 8 bytes
```
ƛdɾ<;fIt
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJzIiwiIiwixptkyb48O2ZJdCIsIiIsIjUiXQ==)
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 41 bytes
```
.+
$*
1
$`1$.`$*00
((.)+?)(?<-2>.)+$
$1
1
```
[Try it online!](https://tio.run/##DcY7CoAwEEDB/p1jhXwwZOMfxJReIxYWNhbi/aNTzXO@133UxuylBo84FCkqoYiLEWOC9dmavLZp@yuIorUqiY6egZGJmQWNHw "Retina 0.8.2 – Try It Online") Link includes test cases. Explanation:
```
.+
$*
1
$`1$.`$*00
```
Create the string `101100111000`... up to `n` `1`s and `n` `0`s, which is twice as long as the desired triangle.
```
((.)+?)(?<-2>.)+$
$1
```
Delete the second half of the string.
```
1
```
Count the number of `1`s remaining.
[Answer]
# [J](http://jsoftware.com/), 25 bytes
```
(1#.2&!$&;1 0<@#"{~i.)@>:
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/NQyV9YzUFFXUrA0VDGwclJWq6zL1NB3srP5rcnGlJmfkK6QpGShoGCpoK2TqKRgaGGj@BwA "J – Try It Online")
```
(1#.2&!$&;1 0<@#"{~i.)@>:
(                    )@>. increment n by 1 and then
                   i.     for every i in 0 … n+1:
          1 0  #"{~         take i 1s and i 0s,
             <@             and box the result (;1 0;1 1 0 0;…)
    2&!                   T(n) by binominal(n+1, 2)
       $&;                raze the boxes to a list (1 0 1 1 0 0…)
                            and take the first T(n) elements
 1#.                      sum the list, i.e. count the 1s
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~15~~ 14 bytes
```
:"@:t~]vG:s:)z
```
Input is 1-based.
[Try it online!](https://tio.run/##y00syfn/30rJwaqkLrbM3arYSrPq/39TAA) Or [verify the first values](https://tio.run/##y00syflvpeQQ4QUirUrqYsu8rIqtNKv@u/w3NAQA).
### How it works
```
       % Implicit input: n
:      % Range: [1 2 ... n].
"      % For each
  @    %   Push iteration index, k (goes from 1 to n)
  :    %   Range: gives [1 2 ... k]
  t    %   Duplicate
  ~    %   Negate, element-wise: gives [0 0 ... 0] (k zeros)
]      % End
v      % Concatenate everything into a column vector
G      % Push n again
:      % Range: [1 2 ... n]
s      % Sum: gives n-th triangular number, T(n)
:      % Range
)      % Index: keeps the first T(n) values
z      % Number of nonzeros
       % Implicit output
```
[Answer]
# [R](https://www.r-project.org/), 55 bytes
```
sum(unlist(Map(rep,list(1:0),e=n<-1:scan()))[1:sum(n)])
```
[Try it online!](https://tio.run/##K/r/v7g0V6M0LyezuETDN7FAoyi1QAfMMbQy0NRJtc2z0TW0Kk5OzNPQ1NSMBjKByvM0YzX/Gxr@BwA "R – Try It Online")
Generates A118175 and sums the first \$T(n)\$ terms.
[Answer]
# Desmos, 85 bytes
\$\sum\_{n=1}^{x(x+1)/2}(1-\operatorname{ceil}(\sqrt{n})+\operatorname{round}(\sqrt{n}))\$
```
\sum_{n=1}^{x(x+1)/2}(1-\operatorname{ceil}(\sqrt{n})+\operatorname{round}(\sqrt{n}))
```
I haven't been able to find a nice formula myself, so I used the \$a(n) = 1 - \operatorname{ceil}(\sqrt{n+1}) + \operatorname{round}(\sqrt{n+1})\$ formula provided on [A118175's page](https://oeis.org/A118175).
[Answer]
# [Gaia](https://github.com/splcurran/Gaia), ~~12~~ ~~11~~ 10 bytes
```
┅2…&¦_2÷eΣ
```
[Try it online!](https://tio.run/##S0/MTPz//9GUVqNHDcvUDi2LNzq8PfXc4v//DQ0B "Gaia – Try It Online")
Uses the observation from [Jonathan Allan's answer](https://codegolf.stackexchange.com/a/214616/67312) to save a byte (so go upvote that), namely that constructing the complement sequence and counting the 1s in the *second* half is equivalent to counting the 1s in the first half.
```
		# implicit input n
┅		# push [1, 2, ..., n]
2…		# push [0,1]
&¦		# for each i in [1, 2, ..., n] repeat each element of [0,1] i times
_2÷		# flatten and divide into two sublists of equal length
eΣ		# take the second sublist and sum
```
[Answer]
# [MathGolf](https://github.com/maxbergmark/mathgolf/blob/master/math_golf.txt), ~~19~~ 12 [bytes](https://github.com/maxbergmark/mathgolf/blob/master/code_page.py)
```
╒♂░*mzyh½<iΣ
```
Outputs the 1-based \$n^{th}\$ value.
[Try it online.](https://tio.run/##y00syUjPz0n7///R1EmPZjY9mjZRK7eqMuPQXpvMc4v//zfkMuIy5jLhMuUy4zLnsuCy5DI04DI05TIy4DIy5TIFsg0MAA)
**Original 19 [bytes](https://github.com/maxbergmark/mathgolf/blob/master/code_page.py) answer:**
```
╒♂░*mzykæî‼<≥]╡imΣΣ
```
Outputs the 1-based \$n^{th}\$ value as well.
[Try it online.](https://tio.run/##AVMArP9tYXRoZ29sZv//4pWS4pmC4paRKm16eWvDpsOu4oC8POKJpV3ilaFpbc6jzqP//zEKMgozCjQKNQo2CjcKOAo5CjEwCjE1CjIwCjI1CjUwCjEwMA)
**Explanation:**
```
╒               # Push a list in the range [1, (implicit) input]
 ♂░             # Push 10, and convert it to a string: "10"
   *            # Repeat the "10" each value amount of times: ["10","1010","101010",...]
    m           # Map over each inner string:
     z          #  Revert sort its digits: ["10","1100","111000",...]
      y         # Join it together to a single string: "101100111000..."
       h        # Push its length (without popping the string itself)
        ½       # Halve it
         <      # Only keep the first length/2 amount of digits in this string
          i     # Convert the string to an integer
           Σ    # And sum its digits
                # (after which the entire stack joined together is output implicitly)
╒♂░*mzy         # Same as above
                # Get its prefixes (unfortunately there isn't a builtin for this):
       k        #  Push the input-integer
        æ       #  Loop that many times,
                #  using the following four characters as body:
         î      #   Push the 1-based loop index
          ‼     #   Apply the following two commands separated:
           <    #    Slice to get the first n items
           ≥    #    Slice to remove the first n items and leave the remainder
        ]       #  After the loop, wrap all values on the stack into a list
         ╡      # Remove the trailing item
          i     # Convert each string of 0s/1s to an integer
           mΣ   # Sum the digits of each inner integer
             Σ  # And sum the entire list together
                # (after which the entire stack joined together is output implicitly)
```
[Answer]
# [Raku](http://raku.org/), 40 bytes
```
{sum flat({1,0 Xxx++$}...*)[^sum 1..$_]}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/urg0VyEtJ7FEo9pQx0AhoqJCW1ulVk9PT0szOg4kZ6inpxIfW/u/OLFSITexAKhLByRmZPAfAA "Perl 6 – Try It Online")
* `{ ... } ... *` is an infinite sequence, where the bracketed expression is a function that generates each successive element.
* `++$` increments the anonymous state variable `$` each time the generating function is evaluated. The first time it's called, `++$` is 1, then 2, etc.
* `1, 0` is just a two-element list.
* `xx` is the replication operator. Prefixed with the cross-product metaoperator `X`, `Xxx` crosses the list `1, 0` with the incrementing value of `++$`, generating the sequence `(((1), (0)), ((1, 1), (0, 0)), ((1, 1, 1), (0, 0, 0)), ...)`.
* `flat` lazily flattens that infinite sequence into the given sequence S, ie: `1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, ...`.
* `[^sum 1..$_]` takes the first N elements from that sequence, where N is the sum of the numbers from 1 up to `$_`, the argument to the function.
* The leading `sum` sums the selected elements.
[Answer]
# [Arn](https://github.com/ZippyMagician/Arn) `-rlx`, [14 bytes](https://github.com/ZippyMagician/Arn/wiki/Carn)
```
&♦r┬f½▀╔î¾rl¥Æ
```
[Try it!](https://zippymagician.github.io/Arn?code=JC4ofHt8YXthPn1cfjorfVw=&input=MQoyCjMKNAo1CjYKNwo4CjkKMTA=&flags=cmx4)
# Explained
Unpacked: `$.(|{|a{a>}\~:+}\`
```
            Mutate STDIN from N → [1, N]
$.                Partition down middle
  (
    |..\            Fold N with concatenation
          _             Where N is a variable; implied
     {                After mapping with block, key of `_`
       |..\
            ~:+             Where N is a one-range to _ * 2
        a{                Block with key of `a`
             a
           >                Is greater than
             _                Implied
        }                 End block
     }                End block   
               Last entry, sum
```
Alternate 14 byte solution with the same flags: `$.(a{~:+a@>a}\):_`
# [Arn](https://github.com/ZippyMagician/Arn), [23 bytes](https://github.com/ZippyMagician/Arn/wiki/Carn)
```
W▀Q$µgˆÎ§Ò"ˆÞC5fbA┐V-7J
```
[Try it!](https://zippymagician.github.io/Arn?code=K3sxLSg6XjovKSs6digwLjUrOi99XH46LSorKw==&input=MQoyCjMKNAo1CjYKNwo4CjkKMTA=&flags=) Thinking of adding a round fix to Arn, that'll help this pretty high byte count.
1-indexed, returns the Nth term. Based off of [@J42161217](https://codegolf.stackexchange.com/users/67961/j42161217)'s answer
# Explained
Unpacked: `+{1-(:^:/)+:v(0.5+:/}\~:-*++`
```
+...\ Fold with addition after mapping
  ~ Over the one-range to
    :-*++ n * (n + 1) / 2
{ Begin map, key of `_`
    1
  - Subtract
    (
      :^ Ceiling
        _ Implied
      :/ Square root
    )
  + Add
    :v(0.5+:/ Round `:/_`, ending implied
} End map
```
[Answer]
# [Swift](https://swift.org), 80 bytes
*Adapted from the [Python 2 answer by @ovs](https://codegolf.stackexchange.com/a/214602/59749)*
```
func a(_ n:Int,_ k:Int=1)->Int{-(~n*n)>k*k*2 ? k+a(n,k+1):max(0,k-(~n)*n/2-k*k)}
```
And the un-golfed form:
```
func a(_ n: Int, _ k: Int = 1) -> Int {
    -(~n*n) > k*k*2
        ? k + a(n, k+1)
        : max(0, k - ~n*n/2 - k*k)
}
```
And here's some sample output.
```
print((1...10).map { a($0) })
// [1, 2, 3, 6, 9, 11, 15, 21, 24, 28]
```
It might actually be better to use a loop instead of recursion. Some limitations with closures (i.e. lambdas) in Swift forced me to use a function decl, which takes up a lot of space. :/
[Answer]
# [CJam](https://sourceforge.net/p/cjam), 22 bytes
```
qi),:+{_)mqmo\mqi-}%:+
```
Uses `round(sqrt(n+1)) - floor(sqrt(n))` to calculate the `n`th position in the bit sequence. (Getting it as a repetition of numbers was smaller to generate, but one byte larger in the end to sum.)
[Try it online!](https://tio.run/##S85KzP3/vzBTU8dKuzpeM7cwNz8mtzBTt1bVSvv/fyMDAA "CJam – Try It Online")
[Answer]
# [Red](http://www.red-lang.org), 109 bytes
```
func[n][b:[[1]]loop n[append/only b head insert next
copy[0 1]last b]sum take/part load form b n + 1 * n / 2]
```
[Try it online!](https://tio.run/##DYw7CsMwEER7n2K6QELwp3SRQ6RdtpCtFTGRV0KWIT69stM8GN5MEd/e4om7MKOFU1dSpmUmGpljShlKLmdR3yeNFxZ8xHlsekipUPnVbk35ogEjR3dULHycO6r7Sp@dKTGZHlLZbap4YMTd2GPiViSLsxNMA6iDJZdNK8iq2/OFYOSO2x8 "Red – Try It Online")
I know it's very long - I just wanted to see what the [K solution](https://codegolf.stackexchange.com/a/214583/75681) (cortesy @coltim) would look like in Red :)
]