28
\$\begingroup\$

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:

This is , so shortest answer as measured in bytes wins.

\$\endgroup\$
5
  • \$\begingroup\$ Related, related, sandbox. \$\endgroup\$ Commented Feb 7, 2022 at 8:28
  • 9
    \$\begingroup\$ Do we have to handle negative inputs? \$\endgroup\$ Commented Feb 7, 2022 at 8:40
  • 1
    \$\begingroup\$ Can we output 2 consistent values instead of 'truthy' and 'falsy'? \$\endgroup\$ Commented Feb 7, 2022 at 10:10
  • 2
    \$\begingroup\$ @DominicvanEssen, I think it's default for decision-problem (see tag info). \$\endgroup\$ Commented Feb 7, 2022 at 10:45
  • 1
    \$\begingroup\$ @hyper-neutrino No, nonnegative integers only. Updated the question to specify this. \$\endgroup\$ Commented Feb 7, 2022 at 19:16

40 Answers 40

1
2
1
\$\begingroup\$

Haskell, 41 38 bytes

f n=or[n==x*x+y*y|x<-[0..n],y<-[0..x]] 

Try it online!

  • Thanks to @ovs for saving 3 Bytes.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ elem n[... saves 2, or[n==... saves 3 \$\endgroup\$ Commented Feb 8, 2022 at 0:16
1
\$\begingroup\$

MATLAB, 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!

\$\endgroup\$
2
  • \$\begingroup\$ I don't really know MATLAB, but I think you should be able to get down to 35 bytes by not assigning to o (so leaving it as an anonymous function definition), swapping (x/2) for just x, and outputting the other way around (so 1 for not sum of 2 squares) by removing ~ and changing to all... \$\endgroup\$ Commented Feb 10, 2022 at 20:36
  • \$\begingroup\$ You're right. I didn't realize my truthy value could be 0, and falsy be 1. Thanks \$\endgroup\$ Commented Feb 14, 2022 at 19:39
1
\$\begingroup\$

F#, 66 bytes

let a t=Seq.allPairs[0..t][0..t]|>Seq.tryFind(fun(f,s)->f*f+s*s=t) 

Try it online!

Straight-forward: create two lists from 0 to total, Seq.allPairs gets the Cartesian product between the two, and Seq.tryFind tries to find the pair that, when squared and added together, equals the total t.

\$\endgroup\$
1
\$\begingroup\$

Desmos, 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!

The trick 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.

\$\endgroup\$
1
\$\begingroup\$

BrainChild, 76 bytes

int n->int=>{int a=0while a<=n{int b=0while b<=a if(n==a*a+b*b++)n=0a++}!n;} 

Try It Online!

Ungolfed with comments

int n->int=>{ // Define a function taking 1 int and returning an int. Explicitely stating return type ends up faster than using `return` later. int a=0 while a<=n{ // Standard for(a=0; a<=n; a++) int b=0 while b<=a // for(b=0; b<=a if(n==a*a+b*b++) // We increment b as we check if a^2+b^2 to avoid needing blocks n=0 // Instead of returning, we just set n to 0, as n=0 is a known truthy state. a++ // And if n is zero, everything conveniently exists early. } !n; // if by the end, n is non-zero, this returns 0, or 1 otherwise. } 
\$\endgroup\$
1
\$\begingroup\$

APL(NARS), 23 chars

{⍵=0:1⋄∨/0=1∣√⍵-×⍨⍳⌊√⍵} 

if input is n>=0, return 1 if n=0 or if n!=0, for some a in 1..√n, sqrt(n-a^2) is one integer. Could be this 15 if one can apply "0 is true", "!=0 is false" and input>0

{×/1∣√⍵-×⍨⍳⌊√⍵} 

Test:

 (0,⍳100)/⍨ {⍵=0:1⋄∨/0=1∣√⍵-×⍨⍳⌊√⍵}¨0,⍳100 0 1 2 4 5 8 9 10 13 16 17 18 20 25 26 29 32 34 36 37 40 41 45 49 50 52 53 58 61 64 65 68 72 73 74 80 81 82 85 89 90 97 98 100 
\$\endgroup\$
1
\$\begingroup\$

SAKO, 75 bytes

PODPROGRAM:F(N) **)LINIIENT(A*2+B*2-N) POWTORZ:A=0(1)N POWTORZ:B=0(1)N WROC 

Outputs 0 carriage returns for falsy and a positive number of carriage returns for truthy. An easy way to check how many were printed is by using this command: ./filename | grep $'\r' -c.

Full programme version, 77 bytes

CZYTAJ:N **1)LINIIENT(A*2+B*2-N) POWTORZ:A=0(1)N POWTORZ:B=0(1)N STOP1 KONIEC 
\$\endgroup\$
0
\$\begingroup\$

JavaScript (V8), 71 bytes

f=n=>(m=n)?[...Array(++m*m).keys()].some(i=>(i%n)**2+(~~(i/n))**2==n):0 

Try it online!

Not great but might as well post it since I spent an hour trying.

\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 6 bytes

Ė2Ṛ²ʂƇ 

Try it online!

Very slow for large inputs.

Explanation

Ė2Ṛ²ʂƇ # Implicit input Ė # Push [0..input] 2Ṛ # Combinations with # replacement with # a length of two ² # Square (vectorised) ʂ # Sum each inner list Ƈ # Contains the input? # Implicit output 
\$\endgroup\$
0
\$\begingroup\$

AWK, 53 bytes

{for(;i<=$1;i++)for(j=-1;j++<$1;)j^2+i^2-$1||g=1}$0=g 

Attempt This Online!

It's not fast when you get to bigger numbers as it's exhaustive and doesn't short when it finds one.

{for(;i<=$1;i++) # all numbers up to input for(j=-1;j++<$1;) # all numbers up to input j^2+i^2-$1||g=1} # it's not a good sum or set output to 1 $0=g # prints a one if a good sum is found 
\$\endgroup\$
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.