A positive integer x is a square triangle number iff there are two different positive integers, y and z, which are smaller than x such that all of the sums

x + y

x + z

y + z

are perfect squares.

For example 30 is a square triangle number because

30 + 6 = 62

30 + 19 = 72

6 + 19 = 52

Your task is to write some code that takes a positive integer as input and determines whether or not it is a square triangle number. You should output one of two distinct values, one if the input is a square triangle number and the other otherwise.

This is so answers will be scored in bytes with fewer bytes being better.


Here are all of the square triangle numbers under 1000


OEIS A242445

  • 6
    \$\begingroup\$ This is OEIS A242445. \$\endgroup\$ – Mr. Xcoder Jan 11 '18 at 16:06
  • \$\begingroup\$ @Mr.Xcoder Thanks! I probably should have checked the OEIS first. I'll add that to the body to make it more searchable. \$\endgroup\$ – Post Rock Garf Hunter Jan 11 '18 at 16:07
  • \$\begingroup\$ For clarification purposes, "...iff there are two different positive integers, y and z, which are smaller than x..." means that y < x and z < x or that y+z < x? \$\endgroup\$ – J. Sallé Jan 11 '18 at 16:19
  • 2
    \$\begingroup\$ @J.Sallé The former \$\endgroup\$ – Post Rock Garf Hunter Jan 11 '18 at 16:20
  • \$\begingroup\$ Here test case with input and output are absent \$\endgroup\$ – RosLuP Jan 13 '18 at 11:01

20 Answers 20


Haskell, 62 bytes

f x|l<-(^2)<$>[1..x]=or[b>x|c<-l,b<-l,a<-l,a-x+b-x==c,c<b,b<a]

Try it online!


Jelly, 12 bytes


Try it online!

How it works

R²_fṖŒcS€Æ²Ẹ  Main link. Argument: x

R             Range; yield [1, 2, ..., x].
 ²            Square; yield [1², 2², ..., x²].
  _           Subtract; yield [1²-x, 2²-x, ..., x²-x].
    Ṗ         Pop; yield [1, 2, ..., x-1].
   f          Filter; keep those values of n²-x that lie between 1 and x-1.
              This list contains all integers n such that n+x is a perfect square.
              We'll try to find suitable values for y and z from this list.
     Œc       Yield all 2-combinations [y, z] of these integers.
       S€     Take the sum of each pair.
         Ʋ   Test each resulting integer for squareness.
           Ẹ  Any; check is the resulting array contains a 1.

Python 2, 93 87 86 bytes

-1 byte thanks to Dennis

lambda n:{0}in[{m**.5%1for m in[x+y,n+x,n+y]}for x in range(1,n)for y in range(1+x,n)]

Try it online!


Brachylog, 19 bytes


Try it online!

Also 19 bytes: ~hṪ>₁ℕ₁ᵐ≜{⊇Ċ+}ᶠ~^₂ᵐ


~hṪ                    Ṫ = [Input, A, B]
  Ṫ>₁                  Ṫ is strictly decreasing (i.e. Input > A > B)
  Ṫ  ℕ₁ᵐ               All members of Ṫ are in [1, +∞)
  Ṫ     ≜              Assign values to A and B that fit those constraints
  Ṫ      ¬{       }    It is impossible for Ṫ…
           ⊇Ċ            …that one of its 2-elements subset…
            Ċ+           …does not sum…
              ¬~^₂       …to a square

PowerShell, 150 bytes

param($x)filter f($a,$b){($c=[math]::Sqrt($a+$b))-eq[math]::Floor($c)}1..($i=$x-1)|%{$y=$_;1..$i|%{$o+=+($y-ne$_)*(f $x $y)*(f $x $_)*(f $y $_)}};!!$o

Try it online! or Verify some test cases

Takes input $x. Establishes a filter (here equivalent to a function) on two inputs $a,$b, which returns a Boolean true iff the [math]::sqrt of $a+$b is -equal to the Floor of that square root (i.e., it's an integer square root).

The rest of it is the meat of the program. We double-for loop up from 1 to $x-1. Each iteration, we check whether $y is -notequal to $_ (i.e., $z), and whether the function is true for all combinations of $x, $y and $_. If it is, $o is incremented by one (which makes it non-zero).

Finally, at the end, we double-Boolean-negate $o, which turns 0 into False and non-zero into True. That's left on the pipeline and output is implicit.


Haskell, 75 69 bytes

f x=or[all(`elem`map(^2)[1..x])[x+y,x+z,y+z]|y<-[1..x-1],z<-[1..y-1]]

Try it online!

Could probably be improved if someone knows a shorter way to test if a number is square. I'm pretty sure using sqrt ends up being longer because floor turns the result into an integral type so you need to put fromIntegral in somewhere before you can compare to the original.

EDIT: Thanks @Wheat Wizard for taking off 6 bytes!


JavaScript (ES7), 75 71 bytes

<input type=number min=1 oninput=o.textContent=f(+this.value)><pre id=o>

  • \$\begingroup\$ Seems like you ninja'd me by 2 minutes. :) Ours answers are very close, so shall I delete mine? \$\endgroup\$ – Arnauld Jan 11 '18 at 17:14
  • \$\begingroup\$ @Arnauld No, I'm sure you arrived at your solution independently. \$\endgroup\$ – Neil Jan 11 '18 at 17:32

05AB1E, 18 bytes


Try it online!

Thanks to Emigna for  -3  -1 byte and a fix!

  • \$\begingroup\$ You don't need the 2 's as both n and O vectorizes. This also doesn't work as the final 2 bytes will return true for any list with at least 1 value, even if only contains false values. This can be fixed (and shortened) by using Z instead. \$\endgroup\$ – Emigna Jan 12 '18 at 7:04
  • \$\begingroup\$ @Emigna Thank you! (BTW I did need €O and that’s why the previous approach did work with ) \$\endgroup\$ – Mr. Xcoder Jan 12 '18 at 7:14
  • \$\begingroup\$ It didn't work though. Check for example 45, that should return false. \$\endgroup\$ – Emigna Jan 12 '18 at 7:16
  • \$\begingroup\$ Um huh, ok. Anyway, updated now. Thanks \$\endgroup\$ – Mr. Xcoder Jan 12 '18 at 7:17
  • \$\begingroup\$ @Sanchises Fixed. Thanks \$\endgroup\$ – Mr. Xcoder Jan 12 '18 at 16:12

R, 79 bytes


Try it online!

computes all the values of y,z with y<-(z=s-x)[z>0&z<x], then computes all their sums with outer(y,y,"+"). This yields a square matrix where off-diagonal entries are potentially squares, as y==z only if they are on the diagonal. Hence, diag(S)=0 sets the diagonals to zero, which are not perfect squares, and we test to see if any element of S is %in%s.


SWI-Prolog, 88 bytes


Try it online!

s(A, B, C) :-
    between(A, B, C), % Find an integer C between A and B (inclusive),
    C < B,            % which is less than B.
    between(1, B, X), % Find an integer X between 1 and B (inclusive),
    B+C =:= X*X.      % of which (B+C) is the square.
g(X) :-
    s(1, X, Y), % Find Y: 1 <= Y < X, and X+Y is a perfect square
    s(Y, X, Z), % Find Z: Y <= Z < X, and X+Z is a perfect square
    s(Y, Z, Y). % Make sure that Z > Y and Y+Z is a perfect square

g(X) is the rule that takes an integer as parameter and outputs whether it is a square triangle number (true/false).


JavaScript (ES7), 72 bytes

Returns 0 or 1.



let f=


for(n = 1; n < 150; n++) {
  f(n) && console.log(n)


C, 113 bytes


Returns 0 if the number is square triangle, 1 otherwise.

Try it online!

  • \$\begingroup\$ I'm guessing the return(int)sqrt(n)==sqrt(n) is being parsed as return((int)sqrt(n))==sqrt(n) as opposed to the more obvious return(int)(sqrt(n)==sqrt(n))? If not can you explain what p is doing? \$\endgroup\$ – MD XF Jan 12 '18 at 3:16
  • \$\begingroup\$ @MDXF Type cast has higher precedence than ==, so the expression is parsed as ((int)sqrt(n))==sqrt(n) like you guessed. \$\endgroup\$ – Steadybox Jan 12 '18 at 7:51

APL (Dyalog), 49 bytes


Try it online!


Jelly, 15 bytes


Try it online!


ṖŒc;€ŒcS€Æ²ẠƊ€Ẹ || Full program.
Ṗ               || Popped range. Yields [1, N) ∩ ℤ.
 Œc             || Pairs (two-element combinations).
   ;€           || Append N to each.
            Ɗ€  || For each of the lists, check whether:
           Ạ    || ... All ...
       S€       || ... The sums of each of their...
     Œc         || ... Disjoint Pairs
         Ʋ     || ... Are perfect squares.
              Ẹ || Test whether there is any value that satisfies the above.    

Clean, 95 88 86 bytes

import StdEnv

Try it online!


Ruby, 73 bytes


Try it online!


Julia 0.6, 61 bytes

Start reading from the function all. The first argument is an anonymous function checking that the square root of a number is an integer, this is applied to each value in the second argument. The single argument to any is a Generator with two for loops, which for each iteration contains the output of the all function.

Thanks to Mr Xcoder for -2 bytes.

x->any(all(x->√x%1==0,[x+y,x+z,y+z])for y=1:x-1for z=1:y-1)

Try it online!


Pyt, 63 bytes


Tests all possible combinations of y,z such that 1≤z<y<x

Returns 1 if x is a square triangle number, 0 otherwise

Try it online!


MATL, 20 19 18 bytes


Try it online! Returns 1 for falsey, 0 for truthy.

Testcases up to 500: Try it online! (using H instead of G). Runtime is quadratic in the input size, so enumerating the testcases from 1 to n runs in O(n^3), which is why enumerating all testcases up to 1000 times out on TIO.

  • -1 byte and a conjecture less thanks to @LuisMendo
  • -1 byte by a more clever check of integer-ness.

Removing q generates a sequence with the desired sequence as a subset, but without the constraint that y and z be strictly smaller than x. An example is x=18, y=7, z=18.

q:    % Push 1...n-1
2XN   % Generate all permuations of choosing 2 numbers from the above.
!     % Transpose to take advantage of column-wise operators later on.
 G+   % Add n to these combinations, to have all combos of x+y and x+z
t  ws % Duplicate the combinations, swap to the top of the stack and sum to get y+z.
v     % Concatenate vertically. The array now contains columns of [x+y;x+z;y+z].
X^    % Element-wise square root of each element
1\    % Get remainder after division by 1.
a     % Check if any have remainders, columnwise. If so, it is not a square triangle.
A     % Check whether all combinations are not square triangle.
  • \$\begingroup\$ @LuisMendo Thanks. Too bad, I was hoping for an answer to my conjecture, but I can't just ask it at Math.SE without showing some effort for a proof... \$\endgroup\$ – Sanchises Jan 12 '18 at 23:27
  • 1
    \$\begingroup\$ qchu.wordpress.com/2009/07/02/… here you go \$\endgroup\$ – mudkip201 Jan 13 '18 at 15:06

APL NARS, 340 bytes

r←h n;i;j;k
A: →C×⍳k≤i+←1⋄j←i+1
B: →A×⍳j>k⋄→0×⍳0=1∣√(i⊃l)+j⊃l⋄j+←1⋄→B
C: r←0


      :for i :in ⍳100⋄k←h i⋄:if 1=k⋄⍞←' ',i⋄:endif⋄:endfor⋄⎕←' '
  30  44  47  48  60  66  69  70  78  86  90  92  94  95  96  98 
 ¯5 ¯1  ¯4 ¯1  ¯3 ¯1  ¯2 ¯1  ¯1 ¯1  0 ¯1  1 0  2 0  3 0  4 0  5 0 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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