[Question]
  [
[Related OEIS sequence: A008867](http://oeis.org/A008867)
## Truncated triangular number
A common property of triangular numbers is that they can be arranged in a triangle. For instance, take 21 and arrange into a triangle of `o`s:
```
     o 
    o o
   o o o
  o o o o
 o o o o o
o o o o o o
```
Let's define a "truncation:" cutting triangles of the same size from each corner. One way to truncate 21 is as follows:
```
     . 
    . .
   o o o
  o o o o
 . o o o .
. . o o . .
```
(The triangles of `.` are cut from the original).
There are 12 `o`s remaining, so 12 is a truncated triangle number.
# Task
Your job is to write a program or a function (or equivalent) that takes an integer and returns (or use any of the standard output methods) whether a number is a truncated triangle number.
# Rules
* No standard loopholes.
* The input is a non-negative integer.
* A cut cannot have a side length exceeding the half of that of the original triangle (i.e. cuts cannot overlap)
* A cut can have side length zero.
# Test cases
Truthy:
```
0
1
3
6
7
10
12
15
18
19
```
Falsy:
```
2
4
5
8
9
11
13
14
16
17
20
```
**Test cases for all integers up to 50: [TIO Link](https://tio.run/##DchNC8IgGADgvyIMZB@OfUS3CesS7BbVTQxeliyxvYUzIsT9ddvpgWcG91AzOD1CjOnJanRV76/2o1jCjvBcNtuQ0b4/K3iLy@srttd4H3BxgKMSCeGcYJ5iucuqlpTE3NrC5Fg0lNad6ZARj8wERgZ0alJ2kZxzHyRdD9bCb93XUoiykTLGPw)**
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so submissions with shortest byte counts in each language win!
  
[Answer]
## Haskell, ~~46~~ 45 bytes
```
f n|t<-scanl(+)0[1..n]=or[x-3*y==n|x<-t,y<-t]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P00hr6bERrc4OTEvR0Nb0yDaUE8vL9Y2vyi6QtdYq9LWNq@mwka3RKcSSMT@z03MzFOwVSgoyswrUVBR0NDRtNGyS7NRsYs20NMzMoj9DwA "Haskell – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 46 bytes
```
f n=or[mod(gcd(p^n)(4*n-1)-5)12<3|p<-[1..4*n]]
```
[Try it online!](https://tio.run/##DcgxCoAgFADQqzg0aOBHrbY6iRhIZkn2FXPs7tYb32mfa4@xNU9wSUXfydFjczSvyOjYI5eMT0yqeXjzzLUE@NOYdtuASy4Ba@dDrHshFFMFz4gWAEoI0z4 "Haskell – Try It Online")
Having thrown a bunch of number theory at the problem (thanks @flawr), I found this characterization:
> 
> **n** is a truncated triangular number exactly if in the prime factorization of **4n-1**, any prime of the form **5 mod 12** or **7 mod 12** appears an even number of times.
> 
> 
> 
This means, for instance, that **4n-1** may not be divisible by 5 unless it's further divisible by **52=25** and the total number of **5** factors is even.
Haskell doesn't have a factorization-built-in, but we can improvise. If we work with factorizations into primes powers like **12=3\*4**, we can use the equivalent statement:
> 
> **n** is a truncated triangular number exactly if the factorization of **4n-1** into prime powers has no terms of form **5 mod 12** or **7 mod 12**.
> 
> 
> 
We can extract the power of a prime **p** appearing in **k** as `gcd(p^k)k`. We then check that the result **r** is not 5 or 7 modulo 12 as `mod(r-5)12>2`. Note that **r** is odd. We also check composites as **p**, lacking a way to tell them from primes, but the check will pass as long as its factors do.
Finally, negating `>2` to `<3` and switching `True`/`False` in output saves a byte by letting us use `or` instead of `and`.
---
A related characterization is that the divisors of **4n-1** taken modulo 12 have more total 1's and 11's than 5's and 7's.
**53 bytes**
```
f n=sum[abs(mod k 12-6)-3|k<-[1..4*n],mod(4*n)k==1]<0
```
[Try it online!](https://tio.run/##DckxDoQgEADAr2xhAebYgF6s5CWEAqNEAqxGsPPvaDfJ7K7ELaXWPJAudzZuKSwfK0RQg5i4GJ84C6MQ/z3Z3zfsA49aKzvLll0gfV6BaudDqtsFjI6KnoORiIOUtr0 "Haskell – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 52 bytes
```
f=lambda n,b=1:b>n+1or(8*n-2+3*b*b)**.5%1>0<f(n,b+1)
```
[Try it online!](https://tio.run/##DcpBCoMwEAXQq8ymkEyqZCyFIupFpIsMmjbQ/kjIpqdPfet3/Oo7Y2gtzp/w1S0QrjrLqAuc5GIejG5wN1ZWy9zfL7L4KZozObHtKAmVVlDMhUAJVAJeuxHvLaVIyJXObJ/tDw "Python 2 – Try It Online")
Outputs `True`/`False` flipped. Uses this characterization:
> 
> **n** is a truncated triangular number exactly if **8n-2** has form **a2-3b2** for some integers **a,b**.
> 
> 
> 
We check whether any `8*n-2+3*b*b` is a perfect square for any `b` from `1` to `n+1`. We avoid `b=0` because it gives an error for a square root of a negative when `n==0`, but this can't hurt because only odd `b` can work.
Done non-recursively:
**[Python 2](https://docs.python.org/2/), 53 bytes**
```
lambda n:0in[(8*n-2+3*b*b)**.5%1for b in range(~n,0)]
```
[Try it online!](https://tio.run/##Rcq9DkAwFAbQV7mLpL1@UkQiEk@CoQ2lCR9pLBavXkzmc47rXHYUwbZ9WPVmRk1olEMnakZaxCUbNpI5q6Lc7p4MOZDXmCdxI1FyCId3OKkDfYyfc6UkOUtW4F0P "Python 2 – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 10 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
0r+\ð_÷3f⁸
```
A monadic link accepting an integer and returning a truthy value (a non empty list) or a falsey value (an empty list).
**[Try it online!](https://tio.run/##ASEA3v9qZWxsef//MHIrXMOwX8O3M2bigbj/w4fFkuG5mP//MTI "Jelly – Try It Online")** (footer performs Python representation to show the `[0]` results as they are)  
...or see a [test-suite](https://tio.run/##y0rNyan8/9@gSDvm8Ib4w9uN0x417gByjQyO7jFUMDjcbv@oaY2K@38A "Jelly – Try It Online") (runs for 0 to 20 inclusive)
### How?
Given N, forms the first N triangle numbers, subtracts N from each, divides each result by 3 and keeps any results that are one of the first N triangle numbers.
```
0r+\ð_÷3f⁸ - Link: integer, N             e.g. 7
0r         - zero inclusive range N            [    0, 1, 2,   3,    4, 5,   6,   7]
  +\       - cumulative reduce with addition   [    0, 1, 3,   6,   10,15,  21,  28]
    ð      - start a new dyadic link with that, t, on the left and N on the right
     _     - t subtract N (vectorises)         [   -7,-6,-3,  -1,    3, 8,  14,  21]
      ÷3   - divide by three (vectorises)     [-2.33,-2,-1.33,-0.33,1,2.67,4.67, 7]
         ⁸ - chain's left argument, t          [    0, 1, 3,   6,   10,15,  21,  28]
        f  - filter keep                       [                     1             ]
                                               - a non-empty list, so truthy
```
[Answer]
# [R](https://www.r-project.org/), ~~45~~ 43 bytes
*-2 bytes thanks to [Vlo](https://codegolf.stackexchange.com/users/30693/vlo)*
```
(n=scan())%in%outer(T<-cumsum(0:n),3*T,"-")
```
[Try it online!](https://tio.run/##K/r/XyPPtjg5MU9DU1M1M081v7QktUgjxEY3uTS3uDRXw8AqT1PHWCtER0lXSfO/icV/AA "R – Try It Online")
I'm fairly sure we only need to check the first `n` triangular numbers for this; brute force checks if `n` is in the pairwise differences of the triangular numbers and their triples.
[Answer]
# [Pyt](https://github.com/mudkip201/pyt), 10 [bytes](https://github.com/mudkip201/pyt/wiki/Codepage)
```
Đř△Đ3*ɐ-Ƒ∈
```
[Try it online!](https://tio.run/##ARwA4/9weXT//8SQxZnilrPEkDMqyZAtxpHiiIj//zIw)
Explanation:
```
        Implicit input
Đ       Duplicate input
ř       Push [1,2,...,input]
△       For each element k in the array, get the kth triangle number
Đ       Duplicate the top of the stack
3*      Multiply by 3
ɐ       ɐ - All possible:
 -                       subtractions between elements of the two arrays  
Ƒ       Flatten the nested array
∈       Is the input in the array
```
[Answer]
# [Haskell](https://www.haskell.org/), 48 bytes
```
f a|u<-[0..a]=or[x^2+x-3*(y^2+y)==2*a|x<-u,y<-u]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P00hsabURjfaQE8vMdY2vyi6Is5Iu0LXWEujEsio1LS1NdJKrKmw0S3VqQQSsf9zEzPzbAuKMvNKVNIyc0pSixTSQJqNDGL/AwA "Haskell – Try It Online")
[Answer]
# [J](http://jsoftware.com/), 22 bytes
```
e.[:,@(-/3*])2![:i.2+]
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/U/WirXQcNHT1jbViNY0Uo60y9Yy0Y/9rcvk56Sk4JienFpSkpiiUJeaUphZzpSZn5CtoKNcppCmklqUWVWoqZOqZGoCVBqVmpSZjU6qr54Ci@j8A "J – Try It Online")
Straightforward and somewhat-poorly-golfed approach.
# Explanation
```
e.[:,@(-/3*])2![:i.2+]
             2![:i.2+]  Range of triangular numbers up to N
      (-/3*])           All possible subtractions of 3T from T 
                        where T is triangular up to the Nth triangular number
    ,@                  Flattened into a single list
e.                      Is N in the list?
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), 12 bytes
```
tQ:qYst!3*-m
```
Outputs `1` for truthy, `0` for falsy.
[Try it online!](https://tio.run/##y00syfn/vyTQqjCyuETRWEs39/9/QwMA) Or [verify all test cases](https://tio.run/##DcYrDoAwFEVBf3aBJSHp7R80GyDB4MBTQej@H4yadvXbTuvb8hxvH8I4NVt3c4hApqC/HiVU0YwnkqjMSCigiDIqePcB).
### How it works, with example
Consider input `6`
```
t      % Implicit input. Duplicate
       % STACK: 6, 6
Q:q    % Increase, range, decrease element-wise. Gives [0 1 ... n]
       % STACK: 6, [0 1 ... 6]
Ys     % Cumulative sum
       % STACK: 6, [0 1 3 6 10 15]
t!     % Duplicate, transpose
       % STACK: 6, [0 1 3 6 10 15], [0;
                                     1;
                                     3;
                                     6;
                                     10;
                                     15]
3*     % Times 3, element-wise
       % STACK: 6, [0 1 3 6 10 15 21 28 36 45 55], [0;
                                                    3;
                                                    9;
                                                    18;
                                                    30;
                                                    45]
-      % Subtract, element-wise with broadcast
       % STACK: 6, [  0   1   3   6  10  15  21;
                     -3  -2   0   3   7  12  18;
                     -9  -8  -6  -3   1   6  12;
                    -18 -17 -15 -12  -8  -3   3;
                    -30 -29 -27 -24 -20 -15  -9;
                    -45 -44 -42 -39 -35 -30 -24;
                     -63 -62 -60 -57 -53 -48 -42]
m      % Ismember. Implicit display
       % STACK: 1
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~65 57 52~~ 48 bytes
```
->n,b=0{b+=1;(8*n-2+3*b*b)**0.5%1==0||b<n&&redo}
```
[Try it online!](https://tio.run/##NYy7DoIwAEV3voKQSLSIaWkbn3X0C9wMgw0SB1NJoQMRv70@ejud3EeOdXr0rfLl0Sy1oi9dKLafb4gpq4ITTfSCELqSM6YUnSZ9MHlub83z7Ts39Gl2tm64j7ss6dL2Qus/WAAPWKOM4xYrelGBMlD@8ledBP/p@uijHk8c4WEChK9C5pH4CWRJa/8B "Ruby – Try It Online")
Inspired by [xnor's python answer](https://codegolf.stackexchange.com/a/157335/77598)
[Answer]
# [Python 3](https://docs.python.org/3/), 84 bytes
```
lambda n:1in set([((8*(n+(i*(i+1)/2)*3)+1)**0.5)%4==1for i in range(n)])or n in[0,1]
```
[Try it online!](https://tio.run/##XctBCsIwEIXhdXuK2QgzqWhjLUghJ6ldRGzsQDMJMRtPH@PW3cf/ePGTtyBDYR9DyuBt3lpn7mW3/vG0IJNmgfeacUa8KZQOWSF3ms4XUgNVKNWfRjpcjdEuJGCoh2TltaLQQrVILXN/1Ev520dNU9uwA4f8UxMTS64uXw "Python 3 – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 11 bytes
```
ÅT3*+8*>Å²Z
```
[Try it online!](https://tio.run/##DcY9CoQwEIDR/juFWPoDmWSGmMZTWG23C3sFIWDrhbyAYA4WA694zr4/@ddjz7mWcwvDuAxrOZ/rU3Nf7m5eu3L3U3UIgYi0JEJEPWqYx2MkRJGIV0KTUMXcCw "05AB1E – Try It Online")
**Explanation**
```
ÅT            # get a list of triangle numbers upto input
  3*          # multiply each by 3
    +         # add input to each
     8*       # multiply each by 8
       >      # increment each
        Å²    # check each for squareness
          Z   # max
```
This is based on the fact that a number **T** is triangular if `8T+1` is an odd perfect square.  
We start on the list of triangles we could truncate, calculate the possible larger triangles based on them and check if it in fact is triangular.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 16 bytes
```
ò å+ d@Zd_-3*X¶U
```
[Try it](https://ethproductions.github.io/japt/?v=1.4.5&code=8iDlKyBkQFpkXy0zKli2VQ==&input=OA==) | [Check all test cases](https://ethproductions.github.io/japt/?v=1.4.5&code=y/Ig5SsgZEBaZF8tMypYtkQ=&input=WzAsMSwzLDYsNywxMCwxMiwxNSwxOCwxOSwyLDQsNSw4LDksMTEsMTMsMTQsMTYsMTcsMjBd)
---
## Explanation
```
                     :Implicit input of integer U
ò                    :Range [0,U]
  å+                 :Cumulative reduction by addition
     d@              :Does any X in array Z return true when passed through this function?
       Zd_           :  Does any element in Z return true when passe through this function?
          -3*X       :    Subtract 3*X
              ¶U     :    Check for equality with U
```
---
[Answer]
# [Add++](https://github.com/cairdcoinheringaahing/AddPlusPlus), 36 bytes
```
D,g,@,.5^di=
L,ßR¬+A↑>3€*A€+8€*1€+ºg
```
[Try it online!](https://tio.run/##S0xJKSj4/99FJ13HQUfPNC4l05bLR@fw/KBDa7QdH7VNtDN@1LRGyxFIaFuAWIYg1qFd6f9VchJzk1ISFQztFAzNufy5kPkWXP7/AQ "Add++ – Try It Online")
]