g | x | w | all
Bytes Lang Time Link
046Python 3.8 prerelease250102T004953ZV_R
025AWK250103T155934Zwobtax
022JavaScript V8250102T145528Zdoubleun
008Uiua250101T100856Zlolad
018Wolfram Language Mathematica250102T075543ZIntroduc
026Google Sheets250101T165151Zdoubleun
017x8664 machine code250102T135816Zm90
013Charcoal250102T133031ZNeil
161TSQL setbased250101T191413ZRazvan S
023R250102T003907ZEonema
031Ruby250101T205558ZG B
008Brachylog250101T180636Zcjquines
012APL+WIN250101T173900ZGraham
007Japt250101T120820ZShaggy
045Retina 0.8.2250101T100836ZNeil

Python 3.8 (pre-release), 46 47 48 bytes

-1 thanks to Neil

-1 thanks to ceilingcat

Takes quite a long time for inputs like 2**31-1...

f=lambda n:n*4in[(x*~x)**2for x in range(n+1)]

Try it online!

Explanation for original 48-byte version

f=lambda n:n in[(x*x+x)**2/4for x in range(n+1)]

The xth triangular number is (x*x+x)/2, and we're looking for the square of that: ((x*x+x)/2)**2

We can save two characters by squaring the numerator and denominator separately: (x*x+x)**2/4

We need range(n+1) instead of range(n) to return True when n is 1

AWK, 28 25 bytes

$0=(1+8*$0^.5)^.5%2==1{}1

Try it online!

Explanation: uses the formula in Eonema's answer. $0 is reassigned in the pattern, so it's printed automatically if it's 1; otherwise, we test for the pattern /0/ and print that.

Saved three bytes thanks to Marius_Couet.

Previous solution: 42 41 bytes

{for(b=0;$0>b;b+=a++**3);print($0~b);a=0}

Try it online!

Explanation:

JavaScript (V8), 22 bytes

n=>(8*n**.5+1)**.5%1>0

Try it online!

This is Arnauld's code with -1 by Shaggy, I'm just the messenger. Posting because of lack of a JavaScript answer.

Uiua, 9 8 bytes

∊\+⇡999√

Pad

-1 byte: Tbw - switched from checking \$n\in \sum k^3\$ to \$\sqrt{n}\in \sum k\$

Creates a list of \$\sum_{k=0}^n k \ (0\le n\le 999)\$, then checks membership of \$\sqrt{n}\$. Since the largest squared triangular number under \$2^{31}-1\$ is the 303rd, this range must be 3 digits so we go up to the 999th.

Wolfram Language (Mathematica), 18 bytes 21 bytes

OddQ@√(1+8√#)&

Try it online!

Edit

-3 thanks to att to shorten Sqrt

Google Sheets, 26 bytes 76 bytes

=0=mod(sqrt(1+8*A1^0.5),1)

Expects the integer in cell A1. Returns true or false.

screenshot

See A000537. Thanks to Arnauld.

x86-64 machine code, 17 bytes

31 C9 89 C8 F7 E1 F7 E1 FF C1 29 C7 77 F4 19 C0 C3

Try it online!

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes a 32-bit integer in EDI and returns a 32-bit integer in EAX, which is 0 if the number is in the sequence and -1 if it is not.

In assembly:

f:  xor ecx, ecx    # Set ECX to 0.
r:  mov eax, ecx    # Set EAX to ECX.
    mul ecx         # Multiply EAX by ECX, producing the square.
    mul ecx         # Multiply EAX by ECX, producing the cube.
    inc ecx         # Add 1 to ECX.
    sub edi, eax    # Subtract the cube from the input number.
    ja r            # Jump back, to repeat, if it stays positive.
    sbb eax, eax    # Subtract EAX+CF from EAX, making it -CF.
                    #  CF was set by the previous SUB instruction;
                    #  it's 1 iff the value overflowed to negative.
    ret             # Return.

Charcoal, 13 bytes

№E⊕θX×ι⊕ι²×N⁴

Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - for a sum of cubes, nothing if not. Explanation: Port of my golf to @V_R's Python answer.

   θ            Input `n` as a string
  ⊕             Cast to integer and increment
 E              Map over implicit range
      ι         Current value
     ×          Multiplied by
        ι       Current value
       ⊕        Incremented
    X           Raised to power
         ²      Literal integer `2`
№               Contains
           N    Input `n` as a number
          ×     Multiplied by
            ⁴   Literal integer `4`
                Implicitly print

If potential floating-point accuracy is acceptable, then for 10 bytes:

⁼¹﹪₂⊕×⁸₂N²

Try it online! Link is to verbose version of code. Explanation: Port of @Eonema's R answer.

T-SQL (set-based), 161 bytes

DECLARE @ INT=9 SELECT COUNT(*) FROM (SELECT POWER(n*(n+1)/2,2)s FROM (SELECT TOP 999 ROW_NUMBER()OVER(ORDER BY(SELECT 1))n FROM master..spt_values)x)y WHERE s=@

T-SQL (iterative), 121 bytes

DECLARE @x INT=9DECLARE @ INT=1,@s INT=0 a:SET @s+=@*@*@ IF @s<@x AND @<303 BEGIN SET @+=1 GOTO a END PRINT 1-SIGN(@s^@x)

R, 23 bytes

\(x)(1+8*x^.5)^.5%%2==1

Attempt This Online!

Explanation:

The nth square triangle number is \$x = (\frac{n(n+1)}{2})^2\$, so \$2\sqrt{x}=n^2+n\$, giving \$n=\frac{-1+\sqrt{1+8\sqrt{x}}}{2}\$. Since \$n\$ has to be an integer, that means \$\sqrt{1+8\sqrt{x}}\$ has to be an odd integer, i.e., \${\sqrt{1+8\sqrt{x}}}\mod{2}=1\$.

Ruby, 31 bytes

->n{(1..n).any?{|x|0==n-=x**3}}

Try it online!

Brachylog, 8 bytes

≥ℕ⟦^₃ᵐ+?

Try it online!

Explanation

≥ℕ          Get an integer in [0, input].
  ⟦         Get the range [0, that integer].
   ^₃ᵐ      Map cube over that range.
      +     Get its sum.
       ?    Assert that sum equals the input.

APL+WIN, 12 bytes

Prompts for integer.

⎕∊+\(⍳303)*3

Try it online! Thanks to Dyalog Classic

Japt, 7 bytes

õ³å+ øU

Try it

õ³å+ øU     :Implicit input of integer U
õ           :Range [1,U]
 ³          :Cube each
  å+        :Cumulative sums
     øU     :Contains U?

Retina 0.8.2, 45 bytes

.+
$*
((^1|1\2)+)(?<=(1)*1)(?<-3>\1)*$(?(3)^)

Try it online! Link includes some test cases. Explanation: Converts the input to unary, then searches for a triangular number whose square is the input.