[Question]
  [
Given a nonnegative integer \$n\$, determine whether \$n\$ can be expressed as the sum of two square numbers, that is \$\exists a,b\in\mathbb Z\$ such that \$n=a^2+b^2\$.
```
   0 -> truthy
   1 -> truthy
   2 -> truthy
   3 -> falsy
   4 -> truthy
   5 -> truthy
   6 -> falsy
   7 -> falsy
  11 -> falsy
9997 -> truthy
9999 -> falsy
```
Relevant OEIS sequences:
* [A001481](https://oeis.org/A001481 "Truthy") - should return truthy
* [A022544](https://oeis.org/A022544 "Falsy") - should return falsy
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest answer as measured in bytes wins.
  
[Answer]
# [Python 3](https://docs.python.org/3/), 49 bytes
```
def f(n):[*{(n-2*x*x)**-2for x in range(n+1)}][n]
```
[Try it online!](https://tio.run/##VY/BroJADEX3fEXjamYEI7iDzPZ9wdsRFoMUJcHOpNQEY/x2HuMzGren97an4SZnT4dl6bCHXpEua3NXlBVmNrM2Jit6zzDDQMCOTqhom@tHU1OzjP5k6yYZLsGzwCDI4v04JbFAn0K@3@syAQDhWxl4IFGUInV2AxtdxZPVP/1x44S6WtfuXAhr4kViF@cjBnnVf/n6nXuCGHPTtFrAOqqzvLFWPT3uzrhta1qIZi5tI3vr7gL77noU9XkvZQzoxBb6oZc/ "Python 3 – Try It Online")
Numerics on this and the previous one may be fragile. I don't know whether `n**-2` is guaranteed to give the same value as `(-n)**-2`.
If necessary this can be fixed at the cost of 1 byte.
#### Old [Python 3](https://docs.python.org/3/), 50 bytes
```
def f(n):[*{(n-2*x*x)**-2 for x in range(n+1)}][n]
```
[Try it online!](https://tio.run/##VY9BjoMwDEX3nMLqKkmhKswOlO2cYHaIRSimg0SdyLgSVdWzU0KrqWb7/J/9HW7y6@lrWTrsoVeky9rcFWWFmc2sjckK6D3DDAMBOzqjon2uH01NzTL6s62bZLgEzwKDIIv345REgT5CfjzqMgEA4VsZeCBRlCJ1dgc7XcWb1Yt@u3FCXa1rDy6ENfEm0cX5hEHe@g9f/@c2EGNumtYWsI7qLG@sVVuPuzNu35p2e8WlbWR/dQ@BfXc9ifq8lzIGdGIL/dDLEw "Python 3 – Try It Online")
### [Python 3](https://docs.python.org/3/), 53 bytes
```
def f(n):[*{(n-2*x*x)**2 for x in range(n+1)}-{0}][n]
```
[Try it online!](https://tio.run/##RYxBCoMwFAX3PcXHVX5aIdqd4rYn6E5cSP1phfIMaQoR8ewpoYVuZ4Zxa3gsOKc0iSWrwE2vN4Wy1lFH1romu3iKNIP8iLsoHCvey83sQ48hZYm/rIzh5kBEwa@N8zOCwkkwdQUV3OZ9@6WX8fkSzqXEm7jwi6/@LZw@ "Python 3 – Try It Online")
#### Old [Python 3](https://docs.python.org/3/), 58 bytes
```
def f(n):[*{sorted({x*x,n-x*x})[1]for x in range(n+1)}][n]
```
[Try it online!](https://tio.run/##RY2xCsIwFEV3v@LRKa9WaHBLce0XuIUMxbxoQV5CGiGl9NujQcHlDuccuGFND8/nUiw5cIJR6XZbfExkxZbb3PHpsztqaZyPkGFmiBPfSfBR4m40m1IF/4Xse1QHAEhxVSHOnAR3xPbSQIND/Ri@dJyeC2EtKd8opF98jS/C8gY "Python 3 – Try It Online")
## How
Signals by exit code: Errors out for True and returns without error for False. The error is triggered by trying to access list positions which are out of bounds if `n` is the sum of two squares. In the last two versions the special case a=b triggers a zero division error.
All versions avoid double loops. The first version does it rather clumsily by computing x^2 and n-x^2, taking the maximum and checking for collisions (by counting uniques). The convoluted way of taking the max catches the special case a=b.
The second version implements the same logic a bit smarter: instead of taking the maximum of two values it makes use of he observation that
```
`n = a^2 + b^2`
```
can be rewritten
```
`abs(n-2a^2) = abs(n-2b^2)`
```
instead of the absolute value we can also take squares. AS before we can check for collisions as long as we do not forget the special case a=b.
The last version takes the reciprocal such that the a=b case triggers a zero division error.
[Answer]
# [R](https://www.r-project.org/), 33 bytes
```
function(n)all((n-(1:n)^2)^.5%%1)
```
[Try it online!](https://tio.run/##bYyxCsIwFEX3fsUDKU3gKSZaSwoOHXSSOuhcKKXBQH2pTTL49bGCIIjLHQ7n3CmS9S7crZbuEdqpd/uoA3XeWGLE22FgjJZMlMQbyZtVnqaCR9eO4/BkHVujQIkb3GKOOyxQCFRKFe9RHH@veZIswAY/Bg/GlZAdq9PlkIHRQDMAf@thDsBqkPBpEOwEWV19rfp8/WvGFw "R – Try It Online")
Outputs `NA` if `n` is not the sum of 2 squares, `FALSE` if `n` is the sum of 2 squares.
If we are restricted to `TRUE`/`FALSE` output, then add +4 bytes for [37 bytes](https://tio.run/##K/pfXJqbn2ZUXFiaWJRabPs/rTQvuSQzP08jT1MxMSdHQyNPV8PQKi9Oz1QzzkgTSKmqGmr@L04sKMip1EjWMNAx1DHSMdYx0THVMdMx1zE01LG0tDQHEZaaOihma/4HAA), or +3 bytes for [36 bytes](https://tio.run/##XYzLCsIwFET3/YoLUprAVUy0lhRcuNCVKGjdFkppMFCTmgfi18cIrtzMMHDO2KiNd@FhJHfP0NnBbaMMuvfKaKJpN46E6DlhtW4XJW05TZXnjEbXTdP4Jj1ZIkOOK1xjiRuskDEUQlTfEBT/32mWzcAEPwUPytVQNJfbvgAlQacNp3MD/j5AcsBI4PDTEIyF4rA7XhNsEmFfyg3xAw) if we can reverse the output.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~88~~ ~~74~~ ~~67~~ ~~66~~ ~~64~~ 53 bytes
```
a,b;f(n){for(a=n;~a*n;b=b?--b:--a)n*=a*a+b*b!=n;a=n;}
```
-14 bytes - thanks to Kevin Cruijssen  
-7 bytes - by omitting return (using global variable)  
-1 byte - by using `-` instead of `!=`  
-2 bytes - thanks to AZTECCO  
-11 bytes - thanks to AZTECCO x2
[Try it online!](https://tio.run/##RcrBEoIgFEDRdXxF0gYo2rSTyG95D0WZQWwEpnEc@/TIVm3PvUb2xpSTC8bntjvekxu76/Agf4mp9Q53K3BBZVngq51mBjqoN4igUGMjJdZSAg9Cg4AzCqz2/Fu2MoILjK@EHJ45RVZZduMNTXNOw0JrasHHhXJFtvIx1kMfi3x9AQ "C (gcc) – Try It Online")
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 15 bytes
```
2~SquaresR~#>0&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n6Zg@9@oLriwNLEotTioTtnOQO1/QFFmXkm0soKunUJatHJsrIKagr6DQrWBjoKhjoKRjoKxjoKJjoKpjoKZjoI5UBAoamlpaQ4mLWv/AwA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [APL(Dyalog Unicode)](https://dyalog.com), 14 bytes [SBCS](https://github.com/abrudz/SBCS)
```
{⍵∊∘.+⍨×⍨0,⍳⍵}
```
[Try it on APLgolf!](https://razetime.github.io/APLgolf/?h=AwA&c=q37Uu/VRR9ejjhl62o96VxyeDiQMdB71bgaK1wIA&f=S1Mw4EpTMARiIyA2BmITIDYFYjMgNgfJGQIA&i=AwA&r=tryapl&l=apl-dyalog&m=dfn&n=f)
[Answer]
# [R](https://www.r-project.org/), ~~41~~ 39 bytes
Or **[R](https://www.r-project.org/)>=4.1, 32 bytes** by replacing the word `function` with a `\`.
*Edit: -2 bytes thanks to [@Giuseppe](https://codegolf.stackexchange.com/users/67312/giuseppe).*
```
function(n)n%in%outer(k<-(0:n)^2,k,`+`)
```
[Try it online!](https://tio.run/##K/qfZvs/rTQvuSQzP08jTzNPNTNPNb@0JLVII9tGV8PAKk8zzkgnWydBO0Hzf5qGgSZXTmJBQU6lhqGVuU6aJleahqEhiLS0tDSH0paa/wE "R – Try It Online")
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `RM`, 5 bytes
```
Ẋ²Ṡ=a
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJSTSIsIiIsIuG6isKy4bmgPWEiLCIiLCIwIl0=)
```
Ẋ     # Cartesian product of (implicit range) self with (implicit range) self
 ²    # Square each
  Ṡ   # Sums of each
    a # Do any... 
   =  # Equal the input?
```
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 7 bytes
```
~+Ċ~^₂ᵐ
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/v077SFdd3KOmpodbJ/z/b2lpaf4fAA "Brachylog – Try It Online")
If you put a variable name such as `Z` as argument, you’ll get the actual couple of values whose squared sum is the input.
### Explanation
It’s a direct description of the problem:
```
~+Ċ        Get a couple of two values Ċ whose sum is the input
  Ċ~^₂ᵐ    Both elements of Ċ must be squares of some numbers
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 33 bytes
Returns a positive integer for Truthy, and `0` for Falsey.
```
f=(n,x=1)=>x>n?!n:!(n%x)-f(n,x+2)
```
[Try it online!](https://tio.run/##FYrRCkBAFAXffcXxoPa2COUFy7cIK9K9WtL@/eJpmmn28RmvyW3nnbHMSwjWKE69Kcn0vuch5iZWnHjK7N91RcGKg2IYFC0YHeqfWlMEbBbq@4gwCV9yLPkh6@dteAE "JavaScript (Node.js) – Try It Online")
This is a trivial modification of [@MitchSchwartz's brilliant answer](https://codegolf.stackexchange.com/a/66181/88546) to *[Count sums of two squares](https://codegolf.stackexchange.com/questions/64812/count-sums-of-two-squares)*.
### Explanation
It can be proven that if \$ n \$ has more \$ 4k + 1 \$ divisors than \$ 4k + 3 \$ divisors, that it can be written as the sum of two squares. One way to achieve this would be to add `1` if `n%(4*k+1)==0`, and `-1` if `n%(4*k+3)==0`. Writing this down, we can see that the task comes down to computing the following alternating sum:
```
!(n%1) - !(n%3) + !(n%5) - !(n%7) + ...
```
which can then be written as:
```
!(n%1) - (!(n%3) - (!(n%5) - (!(n%7) - ... )))`
```
The base case of `!n` handles the special case where `n=0`, by returning `1` instead.
[Answer]
# [R](https://www.r-project.org/), 37 bytes
```
function(n,k=2^(0:n)^2)2^n%in%(k%o%k)
```
[Try it online!](https://tio.run/##K/qfZvs/rTQvuSQzP08jTyfb1ihOw8AqTzPOSNMoLk81M09VI1s1XzVb83@ahoEmV05iQUFOpYahlblOmiZXmoahoSbXfwA "R – Try It Online")
This checks whether \$2^n\$ can be written as \$2^{a^2}\times 2^{b^2}\$. This will rapidly run into numerical issues, but is shorter than [pajonk's similar answer](https://codegolf.stackexchange.com/a/242465/86301) because we can then use `%o%` instead of `outer`. Note that [Dominic's R answer](https://codegolf.stackexchange.com/a/242475/86301) remains 4 bytes shorter.
[Answer]
# Regex (ECMAScript or better), 37 bytes
```
^((x(x*))(?=.*(?=\2*$)(\3+$))\4|){2}$
```
[Try it online!](https://tio.run/##TY@9bsIwFEZfhUYI7k0ak1DEgGuYOrAwtGNpJQsujlvjWLaBlJ9nT8NQqcu3nE86Ol/yKMPGaxfz4PSW/L623/TTemHp1Hsl9dI4gLOYj9L2E6CBJkWEhWBpN@tx2kdYP2V9xPXkipfxrd@mozOyWL9Fr60CZMHoDcH0MZ8g8iAKfqq0IQAjPMmt0ZYA8UHYgzF4UcKw4IyOMMyHyPUOwArFDFkVK5yPr1cdVnIFWjjpAy1tBPVefCD@AfoP7LxclLM7xlj5@pQs7VEave15aRXNeklm@K72wPWzIK6zDDth0iTMkyMZQSPby7ipwCNewmDguqQI94qSu0MM0XcXfru1RV5Oi18 "JavaScript (SpiderMonkey) – Try It Online") - ECMAScript  
[Try it online!](https://tio.run/##RU7LTsMwEPyVVbVqvHmQB48DjttUKldO3EixCmokS6YNdpCCjPn14IZKXEY7s7M70x@Mvp3evwANB6dPb3sNmPNARb3dPG1WHrqTYWhFwesVJweqC4xcb9RxgEV7XHAPjRa212pgURal9vPVDuFEplmZlnT4OJvW/2oRdLpHSfz8y4bEvWl0XZFr9HO5EwGLHb/kqgtFVYt5HaYkIac6YCwaIxghmIhA/ECOJieHdrmcy83dQvGS/3VFxb33Uj48bqWcXhgb2RgTsbW4igO0VYzE2usEidqbb3KVx2kqsvKu@AU "Perl 5 – Try It Online") - Perl  
[Try it online!](https://tio.run/##bVJdb9MwFH3vr7hEQ7XT1ksH4gEvmgCBhMQArY9tkdzEab05TrCdNV3pby/XafiY1DzEvvcen@tzfO/Fo5jc5w9HVdaV9XCPMVMVizn8n2m80mdzVq5li5VB3ay0yiDTwjm4FcrAHvqc88Lj8lipHEqskJm3yqxB2LWbLyn4ja22Dj62may9qvDkAOCT0hKK1MhttyURy6pcMqxHlL9vikJamd9JkUsLqw72PEn@nOzDglLe93Vjz7ebwE@IS1eoQeRflJGE0hepabSmqiCOyZ@N0I5El3EcR5Tun0NPDIT4swR7ZPB/GZDgEhlWiHvgbpQOF2YYVs8Pp9zhu/BeWgM27XeotqxDA0c5urGqKi2Fgae0QEbJYZYJY1C6MnXjIYWgts@R2c55WTJlKIdeZwdjG@G@ytbjNTuLAXpDNN4dOU4gg4heYl@fL0FjOaCYq7XyJJrgIyDeg0mw8tl4HAPLamGdxIDoebKkYzDTs0WmpVn7zfUV3EBAwltcpktkzDbCzpdtr6eLzHTJ4Z21YudYobQm7XjYDjtTAIrKBm14jdQkHMx1aqa4jEYo8Fb4bIMOlchmWXmKSDe5Bgf8A5KfJoZtrahJ3yKr6t234k6YtcROydhQGpQWQEpsb3L0LrztE@1NrtCxrVVekvColD@l3jbhff6Va/TQFyR66SK0hPJD@AZhqo4/CGlJG1NKblIW429xFV9Qsng1uqB08foX3V8dLo5hfI7JZPom@Q0 "Java (JDK) – Try It Online") - Java  
[Try it online!](https://tio.run/##NY7RboMwDEXf@Qor4sGmEBE67QEW7UOASdVG20xpgpxUpdr27Yx02otl@foc3fkez97tV3OZPUcI91DyVH4G7zpgzUKI9Q1xwaUgwlcti20MTZETDvtdTjQ8fdNX85Ov22ev2kqNHRhdZ0fPYMG4ZJQhfhjXZmC1lWG2JqKoBGVgjmAnh5ZemhZsr0Zt@3rMIMEuwXxwpwmNi5gCKv82NdJO0eZLAp7k5RDfz8gliEUU7hGkxLQwcyLocdCq@y/jr1He2MQJQ2R0RGtdqef6Fw "Python 3 – Try It Online") - Python  
[Try it online!](https://tio.run/##PY7fS8MwEMff/SvOUliu2tBW8WExSHFFfJkwfWtnUJZ1gZCVNGMVf/zrXTJ1D3dw3/t@vnd29/4xWuCwkK0cOmrkHmblS0mtfFtd/orT6ePD/GlR3ZfPFQPFMwb7jdISNG@l688A1Bp0neZLzqPGRMwvdJ1RmhZLBtKsvMMrtO@0cmSSTvAPoVqa1m0I3haeqT3vsROy3lowoAwEkbqtUCTPkNJgPI3ed8wi0RAlBoH/gGVBUOfhz27n@mNe@Dv3s1XGgfk/EXooIar5TIjxlZCBDAkiueM08a0pkhhJc3URIzbXX/hZfMfjmKX5TXYA "Ruby – Try It Online") - Ruby  
Takes its input in unary, as a string of `x` characters whose length represents the number. Uses a variant of the multiplication/squaring algorithm [explained in this post](https://codegolf.stackexchange.com/questions/125237/its-hip-to-be-square/223201#223201).
Commented and indented:
```
^                  # tail = N = input number
(                  # subtract a perfect square from tail
    # subtract a nonzero perfect square from tail
    (x(x*))        # \2 = any positive integer; \3 = \2-1; tail -= \2
    (?=
        .*         # find the smallest value of tail that satisfies the following
        (?=\2*$)   # assert \2 divides tail
        (\3+$)     # assert \3 divides tail at least once; \4 = \2*\2 - \2
    )
    \4             # along with "tail -= \2" above, acts as "tail -= \2*\2"
|
    # or just subtract 0 from tail
){2}               # do this twice
$                  # assert that tail == 0
```
This can be generalized to sums of any number of squares by changing the `2` in `{2}` to the desired count.
# Regex (Perl / Java / PCRE2 v10.34 or later / .NET), ~~21~~ 20 bytes
```
^(\1?(\2xx|^x)*){2}$
```
[Try it online!](https://tio.run/##RY5fa8IwFMW/ykWCzZ0tTQrbg2m0gnvd095WDU4sBDLtkg4ysuyrd7ETfLncc@6f8@tP1jyOH99ArIBgLseDAVKKJGW93bxuVhG6i6XESSbqlcAAuksKQ2/1eYBZe56JCI2Rrjd6oFmR5e7r3Q3pROUFzzmePq9L67vLko9LolBcf7mUeLCNqSsMjXnjO5kq24lbrr5Joms5jVO3WGDQHVCa@Qw8pCVEkL9QEltiIG4@n@AmtgTOxT8r0SLGqNTzy1apcU9bvqZt5f3P3uMDhiqScWQFf2J/ "Perl 5 – Try It Online") / [Attempt This Online!](https://ato.pxeger.com/run?1=RU5BTsMwEPzKqrIab5socQ4I4bhNpXLlxI20VosayZJpg51KQcZ8hEsP8AM-wzd4AW5SxGV3Z2Z3Z94_mp3Rp--fpxcghoPTh8eNBpLyAEWxXNwvZh7qg6HEiowXM44OVB0QusaofQujaj_iHkotbKNVS6Mkiu1xa9twIuOExQx3z-el-T-bBR5viER-_mWD48aUusjRlfqBrUSo2YpffNUFElWIXg7TdIpO1UBp1EXQQVhCBPEGKTEpOmLH4z5cny0EZ3zIShT33kt5e7eU8vPY1sn115pWbE6rvOte1x1O0OWeDNLpr2UJu8oG8As) - Perl  
[Try it online!](https://tio.run/##bVJNb9swDL3nV3DGhkhpqiY57DDVKLZhAwas29Ae0xRQbDlRK9OeJDdOsvz2jHK8jwL1hSb59Kj3xAf1pM4f8sejKevKBXigXJhKjCT8X2mCsS/WnF7pljqDullak0FmlfdwrQzCHvqaDypQeKpMDiV12G1wBleg3MrPFxzC2lUbD5/aTNfBVHRyAPDZWA1FinrT/bJEZFWuBfUTLj80RaGdzm@0yrWDZQd7XmR/TvZpwbns5/pxkJt15GfMp0vSoPKvBjXj/FWKjbXcFMwL/bNR1rPkYjQaJZzvn0NPDIyFFwn2xBD@MhDBBTEsCfco/Vk6vMNhjEEeTrXDDxWCdggu7f9IbVnHAZ5LcmNZVVYrhF1aEKOWcJspRJJusG4CpBDV9jV2u/VBl8Igl9Dr7GBirfw33Qa6ZmcxQG@IpbsTxwmEhOgl9v35Aiy1I0r42prAknN6BMIHwAl1vmCgNXCiVs5rSpidTxZ8DDh9sSmsxlVYX87gCiIS3lGYLogxWys3X7S9ni7D6ULCe@fU1ovCWMva8bAddqYAFJWL2ugaKU4k4GWKUwpnZyTwWoVsTQ6VxOZEecpYt7lIC/6RyE8bIzZO1awfkVX19ntxo3CladJkjJxHpQWwksZjTt7Ft93x3uSKHNs4EzSLj8rlLg2uie/zr12Th6FgyRufkCVcHuI3iFt1vGd30yt2N2vbX/ctH/H97PD6GLflODmfvp38Bg "Java (JDK) – Try It Online") / [Attempt This Online!](https://ato.pxeger.com/run?1=bVLNbhMxED5wy1MMESh2mmySHhDCXVUFgYREKWq5JankbLyJwetd7Nlm05An4dJD-wY8DU_DeHf5qZS9jGfm22_8fZ4f91_kjbz79QR1VuQOIaSRzqO-gP8rJWpzsObUSlXU6RTlwugEEiO9h3OpLeygrXmUSOEm10vIqMOu0Gm7AulWfjrngGuXbzy8rRJVoM7pzw7AO20UpLFVm_rIuqOzzxejJF-qLhevyzRVTi0vlVwqB4sa9rjI_vzZpinnop3rByg268DPmI8XpEEuP2irGOdPY1saw3XKfKS-ldJ4Gtzv97uc7x5DGwbG8CDBjhjwLwMRjIhhQbivwh_FvZnthYhi39T2nySichZc3J6iJM-KMMBzQW4s8twoaeE2TolRCbhKpLUkXduiRIghqG1r7GrrUWWRtlxAq7OGRWvpP6oK6Zq1xQCtIYbuThwNyBKildj2p3Mw1A6oyBdGI-sO6REIj2DH1HlvkdbARYV0XlHCzHQ85wOwk4PNyCi7wvXJMZxCQMIrCpM5MSZr6abzqtVTZ3YyF3DmnNz6KNXGsGrQq3q1KQBp7oI2ukZsxwLsSWwnFI6OSOC5xGRNDmXE5qKsyVi9uZYW_A2RNxsTbZwsWDsiyYvtRXop7UrRpPHAch6UpsAyGm-X5F1421vempyTYxunUbHwqFzcxujK8D7_2gV5iCnrPvddsoSLffg6YaseSkyHL39es9nklM2Oq-r7dcX7fHe8f9a07sPmNMe7h_Fw8mLcJL8B) - Java  
[Try it on regex101](https://regex101.com/r/lzh7Fq/2) / [Attempt This Online!](https://ato.pxeger.com/run?1=XVDNSsNAEMZrn2JZFrJjkzYJtVq2oRcVCuJBvTXtksZNG0w3YbOFQO0b-AZeCuIb-DQ-jZNWLx52dv6-75uZ949qXR2-z97GE3QIMyQitE9Jj1RGraRRVZGkijv93vlEynVSWJmWmyovlIl5DCJ-6NeOSxx8GSblSrUN2iptay7l7fTuRkpwSQBIicSik-tc1spyWqVG9ZZJ-mINGlnkm9xSl9BBOBqMhpfh6IKC6GSl4SyPfMGKKEP2mj8-XU_vQcCO5Blnxay2plAaPfCCeRTRWFPA5nq7xAqmXd_1AmjxqqmK8llx6lEX2wXi03KrbYsdhwiaIQFafy46hByV9W_M9Dg61tHrdgGlCT9eaJPYdM2ZcVGsPZdKLHcax2UaAMiuHTEHla7Ldi5BcJVAkDYmTIs9yuz_nZWD-NzazLv6WvA4mPA4bJrXRQPnsAv37FQ6_H2-Fwz9U_AD) - PCRE2  
[Try it online!](https://tio.run/##RY/BaoQwFEV/ReSBydgM6qKLCaEDXfcLHAeCfVMD6YvEDIax@XarLaXLc7kc7h3djH4a0NoVvGo9fmDsTifCmZ2L9cou9Qu7NDF@XSM/8KVJsBbnp/zVfY7G4nvOJTxUJW/Oo@4HBjYzlIGh8R74AlqBFdNoTchFLs2NgT727k5B2JA1e6FUoNuqS5uAAanWUOh@EgkkLP5xvXNZ8mV3@OObDv2AEyticQDiv/GDL7M3AcXgppC2WbX850yQ205ZQ5gBpZTWStTP1Tc "PowerShell – Try It Online") - .NET
```
^                  # tail = N = input number
(                  # The following will be captured in \1
    \1?            # Optionally subtract the previous perfect square from tail
    (\2xx|^x)*     # If on the first iteration, subtract a perfect square from tail;
                   # if on the second iteration and "\1?" was evaluated once,
                   # combines with it to subtract a perfect square from tail that
                   # is greater than or equal to the previous one; if "\1?" was
                   # evaluated zero times, this adjusts the first perfect square
                   # subtracted, optionally increasing it to a larger one, which
                   # effectively makes the "other" perfect square zero.
){2}               # Iterate this twice
$                  # Assert that tail == 0
```
[Answer]
# [Python 3](https://docs.python.org/3/), 59 bytes
```
lambda n:n in(n and(i//n)**2+(i%n)**2for i in range(n*n+1))
```
[Try it online!](https://tio.run/##RYpBCoAgFAWv8jfB1wK1dkI3aWOU9aFeIW06vVmbdsPMnPe1Huhy7Ie8hX2cAsGDBAwKmFiMgdK6rVmqD@KRSEqnFLDMDI3aKZVfjV87a5U/k@BiNJFRjgc "Python 3 – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 bytes
```
ŻpŻ²§i
```
[Try it online!](https://tio.run/##y0rNyan8///o7oKjuw9tOrQ8E8g8tPVw@7F2ncPthyc83DHf/f9/IwMA "Jelly – Try It Online")
Outputs `0` for falsy, and a non-zero positive integer for truthy. The Footer in the TIO link simply splits the range \$[0, n]\$ into truthy (top line) and falsey (bottom line)
## How it works
```
ŻpŻ²§i - Main link. Takes n on the left
Ż      - Zero range; [0, 1, ..., n]
  Ż    - Zero range; [0, 1, ..., n]
 p     - Cartesian product
   ²   - Square everything
    §  - Sums of each pair
     i - Index of n, or 0
```
[Answer]
# [tinylisp](https://github.com/dloscutoff/Esolangs/tree/master/tinylisp), 125 bytes
```
(load library
(d V repeat-val
(d R(q((N)(map* *(0to N)(0to N
(q((N)(any(map* contains?(V(map* s(V N(a N 1))(R N))(a N 1))(R N
```
[Try it online!](https://tio.run/##TYy7DsIgGIV3n@KMP01MitfRN2BoE/Zf24EEAYGY8PQI1sHp5Du3bFyxJoVayXpeYM09ciw7WqAR17By3r/Zdp7oRaQEPTkMGGjMHo2@0uP5F7MrW@XhXWbj0o30ZiTSUMRQkELQ1Nbin2pvYUb7GSFxwBEnnHHBFVKifgA "tinylisp – Try It Online")
[Answer]
# [Excel](https://support.microsoft.com/en-us/excel), 64 bytes
```
=LET(x,SEQUENCE(SQRT(A1)+1,,1)^2,1-AND(ISERROR(XMATCH(A1-x,x))))
```
[Link to Spreadsheet](https://1drv.ms/x/s!AkqhtBc2XEHJnhqES3qUXNTMPOcY?e=acImqD)
List all \$k^2\$ in \$[0..n]\$. Match all \$n - k^2\$ to list. If all matches are errors, then false, otherwise true.
[Answer]
# [Desmos](https://desmos.com/calculator), 52 bytes
```
a=.5
f(n)=min(ceil(mod((n-[0...floor(n^a)]^2)^a,1)))
```
[Try It On Desmos!](https://www.desmos.com/calculator/a7t3uxhoc4)
[Try It On Desmos! - Prettified](https://www.desmos.com/calculator/a5k9tooxd3)
### 47 bytes (doesn't work past 100 because of Desmos 10000 element restriction)
```
l=[0...n]
f(n)=min(sign([aa+bb-nfora=l,b=l])^2)
```
[Try It On Desmos!](https://www.desmos.com/calculator/ueloh5qkkp)
[Try It On Desmos! - Prettified](https://www.desmos.com/calculator/mfqgggvtvi)
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~9~~ 8 bytes
```
0y&:U&+m
```
[Try it online!](https://tio.run/##y00syfn/36BSzSpUTTv3/39LAwA "MATL – Try It Online")
([Or this version for larger inputs](https://tio.run/##y00syfn/36AyIk7NKlRNO/f/f0tLS3MA "MATL – Try It Online"))
1 for truthy, 0 for falsy. (Thanks to @Giuseppe for -1 byte and this neater output.)
Square every number from 0 upto the input, add all the combinations of them, and check if the input is in there. (The "larger inputs" version does the sensible - but non-golfy - thing of limiting the check upto the square root of the given number; same logic, two extra bytes, a lot less resource hunger.)
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~8~~ 6 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
ÝãnOIå
```
Bugfixed and -2 bytes thanks to *@Mr.Xcoder*, making it now similar as *@emanresuA*'s Vyxal answer and [*@cairdCoinheringaahing*'s Jelly answer](https://codegolf.stackexchange.com/a/242501/52210).
[Try it online](https://tio.run/##yy9OTMpM/f//8NzDi/P8PQ8v/f/f0AwA) or [verify the smaller test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfeX/w3MPL87zrzy89L/O/2gDHUMdIx1jHRMdUx0zHXMdCx1LHUOgoGEsAA).
**Explanation:**
```
Ý       # Push a list in the range [0, (implicit) input]
 ã      # Create all possible pairs with the cartesian product
  n     # Square each integer
   O    # Sum each inner pair
    Iå  # Check if this list contains the input
        # (after which the result is output implicitly)
```
[Answer]
# [Octave](https://www.gnu.org/software/octave/), 25 bytes
Returns 0 for truthy and 1 for falsy. Quite short considering it's a conventional language.
```
@(n)0||(k=0:n).^2+k.^2'-n
```
[Try it online!](https://tio.run/##y08uSSxL/Z9mq6en999BI0/ToKZGI9vWwCpPUy/OSDsbSKjr5v1PtI2OteZKyy9SyFSwVTCwMjTgAgolWmfqpGlkagKlUvNSuBL/AwA "Octave – Try It Online")
[Answer]
# [tinylisp](https://github.com/dloscutoff/Esolangs/tree/master/tinylisp), ~~125~~ ~~117~~ 109 bytes
```
(d X(q((N I)(i(l N 1)N(X(s N I)(a I 2
(d S(q((I N)(i(a(X I 1)(X(s N I)1))(i(l N I)0(S(a I 1)N))1
(q((N)(S 0 N
```
The last line is an anonymous function that takes a number and returns 1 if it is the sum of two squares, 0 otherwise. [Try it online!](https://tio.run/##PY0xDsMgAAP3vMKjvUGkfiESCwsLK1UWJNqkSZa8ngJSu57u7Cu/75LPvVauiPyQHk7MLPCw8ow8MVCCwzw1K3TLwXcrMTZs9desfrGTYRhVm5FsT5dxIAYY@MqypRUlP4903Jr4SjsW0Fwb5odUvw "tinylisp – Try It Online")
### Ungolfed/explanation
*Look, ma, no library!*
First, we define a helper function `X` that takes a number `N` and determines if it is (not) a square. A perfect square is the sum of consecutive odd numbers; therefore, subtracting the sum of the first several odd numbers from `N` (for an appropriate value of "several") will result in 0 if `N` is square. Thus, we recurse over increasingly large odd numbers (which we track as `I`) and subtract each one from `N` until `N` equals 0 (in which case `N` is square) or `N` is less than 0 (in which case `N` is not square):
```
(def not-square?        ; Define not-square?
  (lambda (N I)         ; as a function of two arguments:
    (if (less? N 1)     ;  If N is less than 1,
      N                 ;  return N (0 if square, negative if not square)
      (not-square?      ;  Else, recurse with these arguments:
        (sub2 N I)      ;   New N is previous N minus current odd number
        (add2 I 2)))))  ;   New I is the next odd number
```
Next, we'll define a function `S` that determines whether a number `N` is the sum of two squares. Our algorithm here is to recurse over integers `I` starting at 0: if `I` is not square, or `N` minus `I` is not square, try the next `I` until `N` is less than `I`, at which point `N` cannot be the sum of two squares. On the other hand, if `I` and `N` minus `I` are both square, then `N` is the sum of two squares.
```
(def sum-squares?           ; Define sum-squares?
  (lambda (I N)             ; as a function of two arguments:
    (if                     ;  If
      (add2                 ;   the sum of
        (not-square?        ;    0 if
          (sub2 N I)        ;    N minus I
          1)                ;    is square, < 0 otherwise
                            ;   and
        (not-square? I 1))  ;    0 if I is square, < 0 otherwise
                            ;  is truthy (nonzero), then:
      (if (less? N I)       ;   If N is less than I
        0                   ;   then return 0
        (sum-squares?       ;   Else, recurse with these arguments:
          (add2 I 1)        ;    New I is the next integer
          N))               ;    Same N
      1                     ;  Else (I and N minus I are both square), return 1
```
Finally, our submission is an anonymous function that takes `N` only and passes it to `sum-squares?` with a starting `I` of 0:
```
(lambda (N)
  (sum-squares? 0 N))
```
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), ~~31~~ 26 bytes
```
.+
$*
^((^1|11\2)*\1?){2}$
```
[Try it online!](https://tio.run/##K0otycxL/K@q4Z7wX0@bS0WLK05DI86wxtAwxkhTK8bQXrPaqFbl/38DLkMuIy5jLhMuUy4zLnMuQ0MuS0tLcxBhCQA "Retina 0.8.2 – Try It Online") Link includes test cases. Edit: Saved 5 bytes thanks to @Deadcode. Explanation: `^((^1|11\2)*)` matches a square number at the beginning of the string. Repeating the expression with `{2}` does not in itself change this, but it allows the use of `\1?` to add a square number matched on the first iteration. (The first stage simply converts the decimal input to unary.)
[Answer]
# [Julia](https://julialang.org/), ~~49~~ 35 bytes
```
~n=1 in[i^2+j^2==n for i=0:n,j=0:n]
```
[Try it online!](https://tio.run/##bZPNbtswEITveQodbVQFNPwTWcB9kSABfAlqw1UD1wHcS17dnW977YEENDNL7g5H54/L6aj74/G5HTSdtufTa/pyfk2Hwza9/bpOp8PybZvP7C@P2/Xj9uPP4XmZJ81TmqcyT3We@jwNI6DZq3mtXoaTsWRFMpasya7JLsr@ztYU88VHFWuK@ervak31OdX1zVyzvplv/l7NreZWY93abr4b63Th@mFs@NzR6YeGFrEVNvpaaDS6jDajz4QkJbbKRm2OYZDQpgoH0KQKWEFCv6JRVSRteXl6ejteftsfg5aBxtGIkXm5huu4LVmVjCfruJMrs7lsPFubwawv@GyMHri94Ju5am3lAaytxumiWt98TnNNowvjzXgzvhpfXbNav9Ke8RVPzXVrO54a78Y7T2rt8DnD3DA3XDfMj3hrhloYfmGshVkXXIkUxNgxdwwekytYagmFmF6ML7KhFA8BiwUiJ8IIkRZhh3K8CxJSI2KjEt6iwxURH@GLCJFquE4t/ohACYdErNQiq7C4pBaPiASTRNJEzIRfWiPUSLBLZE6YJlwTtqlH5jmAJIooCg@FiRrx@rBjOCjv19N2u2y7n8f33f3r98/dfT9P/36v/f7/bGTL5OMv)
Thanks to MarcMush we can save a lot more! We can also replace `f(n)` with the slightly shorter version `~n`!
[Answer]
# [Julia 1.0](http://julialang.org/), ~~28~~ 23 bytes
```
~n=n∈(N=(0:n).^2).+N'
```
[Try it online!](https://tio.run/##bZNNbtRAFIT3OYV32MJErv5zN9JwhFwAgTQbRNAwisJEGjZZ55xcZKjvsWXRLbmqXvd71eUfL6fHo6632@v5cP7z9jY/HObt43m5/5qW@/cP726X55fL99@Hz9s6aZ3SOpV1quvU12kYAc1ezWv3MpyMJSuSsWRNdk12UfZ3tqaYLz6qWFPMV39Xa6rPqa5v5pr1zXzz925uN7cb69Z2891YpwvXD2PD545OPzS0ia2w0ddGo9FltBl9JiQpsVU2anMMg4Q2VTiAJlXAChL6FY2qImnbl7u7b8fTL/tj0DLQOBoxMi/XcB23JauS8WQdd3JlNpeNZ2szmPUFn43RA7cXfDNXra08gLXVOF1U65vPaa5pdGG8GW/Gd@O7a3brd9ozvuOpuW5tx1Pj3XjnSa0dPmeYG@aG64b5EW/NUBvDb4y1MeuGK5GCGDvmjsFjcgVLLaEQ04vxRTaU4iFgsUDkRBgh0iLsUI53QUJqRGxUwlt0uCLiI3wRIVIN16nFHxEo4ZCIlVpkFRaX1OIRkWCSSJqImfBLe4QaCXaJzAnThGvCNvXIPAeQRBFF4aEwUSNeH3YMB@Xp@fF8OZ3nn8en@frh0@t8Xdbp3@@1LP9nI1smb38B "Julia 1.0 – Try It Online")
Based on [David Scholz](https://codegolf.stackexchange.com/a/242869/101725)’s solution.
Thanks to dingledooper for shaving 5 bytes
[Answer]
# [Ruby](https://www.ruby-lang.org/), 44 bytes
```
->n{(k=0..n).any?{|a|k.any?{|b|a*a+b*b==n}}}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y6vWiPb1kBPL09TLzGv0r66JrEmG8pKqknUStRO0kqytc2rra39X6CgAVRoaKipl5tYoKGWpvkfAA "Ruby – Try It Online")
[Answer]
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), 63 bytes
```
lambda n:(r:=range(n+1))and n in(i*i+j*j for i in r for j in r)
```
[Try it online!](https://tio.run/##JcwxCoAwDEDRq2RMWgfFRQrexCVSqykaS3Dx9FV0e/zhl/vaTu2HYjWNU935mCODBrQwGuu6oPqOiDWCgiiKE59dhnQayBvAPuaPVIuJXugOLpga@Add2xJRfQA "Python 3.8 (pre-release) – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 19 bytes
```
Ｎθ≔Ｘ…·⁰θ²η⊙η⊙η⁼θ⁺ιλ
```
[Try it online!](https://tio.run/##S85ILErOT8z5/98zr6C0xK80Nym1SKNQ05rLsbg4Mz1PIyC/HCjgmZecU1qcWZYalJiXnqphoKNQqKmjYATEGUClAUWZeSUajnmVGhk6ClDKtbA0MadYo1BHIQCoUyNTRyFHEwSs//@3tLQ0/69blgMA "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｎθ
```
Input `n`.
```
≔Ｘ…·⁰θ²η
```
Generate a list of squares from `0` to `n²` inclusive (in case `n<2`).
```
⊙η⊙η⁼θ⁺ιλ
```
Check whether any pair sums to `n`.
[Answer]
# [Haskell](https://www.haskell.org/), ~~41~~ 38 bytes
```
f n=or[n==x*x+y*y|x<-[0..n],y<-[0..x]]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P00hzza/KDrP1rZCq0K7UquypsJGN9pATy8vVqcSwqqIjf2fm5iZp2CrkJtY4BuvUFBaElxS5JOnEK1RnJFfrpCnqa2tpKCkrQ3hqigADdWsyYPoNjQwAOoHAA "Haskell – Try It Online")
* Thanks to @ovs for saving 3 Bytes.
[Answer]
# [MATLAB](https://www.gnu.org/software/octave/), 43 bytes
```
o=@(x)any(~mod(sqrt(x-(0:(x/2)^.5).^2),1));
```
taking advantage of vectorized code to search from 0 to sqrt(x/2), checking sqrt(x - i^2) is an integer with mod([],1)
[Try it online!](https://tio.run/##y08uSSxL/f8/39ZBo0IzMa9Soy43P0WjuLCoRKNCV8PASqNC30gzTs9UUy/OSFPHUFPT@v9/AA "Octave – Try It Online")
[Answer]
# k (ngn/k), 37 bytes
```
c:{0<#&x{x=+/y}/:+{(%x)=_%x}#'!2#1+x}
```
try it in the [ngn/k repl](https://ngn.bitbucket.io/k/#r)
[Answer]
# [Desmos](https://desmos.com/calculator), 47 bytes
```
f(n)=\prod_{a=0}^n\prod_{b=0}^n\{aa+bb=n:0,1\}
```
The leading newline is necessary for the piecewise to paste properly.
Outputs 0 for truthy and 1 for falsey.
[Try it on Desmos!](https://www.desmos.com/calculator/fggeb7ut8d)
The [`∏` trick](https://codegolf.stackexchange.com/a/236800/68261) didn't work, maybe due to the `\{aa+bb=n:0,1\}` piecewise. Avoiding it with `sign(aa+bb-n)^2` or `0^{(aa+bb-n)^2}` ended up longer.
]