Count sums of two squares

46

1

Given a non-negative number n, output the number of ways to express n as the sum of two squares of integers n == a^2 + b^2 (OEIS A004018). Note that a and b can be positive, negative, or zero, and their order matters. Fewest bytes wins.

For example, n=25 gives 12 because 25 can be expressed as

(5)^2  + (0)^2
(4)^2  + (3)^2
(3)^2  + (4)^2
(0)^2  + (5)^2
(-3)^2 + (4)^2
(-4)^2 + (3)^2
(-5)^2 + (0)^2
(-4)^2 + (-3)^2
(-3)^2 + (-4)^2
(0)^2  + (-5)^2
(3)^2  + (-4)^2
(4)^2  + (-3)^2

Here are the values up to n=25. Be careful that your code works for n=0.

0 1
1 4
2 4
3 0
4 4
5 8
6 0
7 0
8 4
9 4
10 8
11 0
12 0
13 8
14 0
15 0
16 4
17 8
18 4
19 0
20 8
21 0
22 0
23 0
24 0
25 12

Here are the values up to n=100 as a list.

[1, 4, 4, 0, 4, 8, 0, 0, 4, 4, 8, 0, 0, 8, 0, 0, 4, 8, 4, 0, 8, 0, 0, 0, 0, 12, 8, 0, 0, 8, 0, 0, 4, 0, 8, 0, 4, 8, 0, 0, 8, 8, 0, 0, 0, 8, 0, 0, 0, 4, 12, 0, 8, 8, 0, 0, 0, 0, 8, 0, 0, 8, 0, 0, 4, 16, 0, 0, 8, 0, 0, 0, 4, 8, 8, 0, 0, 0, 0, 0, 8, 4, 8, 0, 0, 16, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 8, 4, 0, 12]

Fun facts: The sequence contains terms that are arbitrarily high, and the limit of its running average is π.

Leaderboard:

    var QUESTION_ID=64812,OVERRIDE_USER=20260;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/64812/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
    body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

xnor

Posted 2015-11-26T00:38:01.640

Reputation: 115 687

4Wait, what?? "The sequence contains terms that are arbitrarily high, and the limit of its running average is π." – Stewie Griffin – 2015-11-26T11:58:04.680

@StewieGriffin The two statements are consistent. Consider the sequence 1,0,2,0,0,3,0,0,0,4,0,0,0,0,5,.... Cutting the sequence off after any nonzero number, the average so far is 1. And, the runs of 0's have less and less impact later in the sequence. – xnor – 2015-11-26T12:04:42.890

5I know it's consistent.. =) I had checked the 10.000 first numbers when I posted the comment. What I don't get is: Why on earth does it equal Pi? – Stewie Griffin – 2015-11-26T12:19:25.630

30@StewieGriffin The sum of the terms up to N corresponds to the points (a,b) with a^2+b^2<=N. These are the lattice points in the circle of radius sqrt(N), whose area is πN. – xnor – 2015-11-26T12:23:05.637

2@xnor and there goes the magic:( – Andras Deak – 2015-11-29T01:02:52.450

@xnor damn, nice connection there. – Lucas – 2015-11-29T21:41:39.450

Answers

20

Python (59 57 56 bytes)

lambda n:0**n+sum((-(n%(x-~x)<1))**x*4for x in range(n))

Online demo

As with my CJam answer, this uses Möbius inversion and runs in pseudoquasilinear time.

Thanks to Sp3000 for 2 bytes' savings, and feersum for 1.

Peter Taylor

Posted 2015-11-26T00:38:01.640

Reputation: 41 901

1Those parentheses are annoying. – lirtosiast – 2015-11-30T22:15:21.227

@ThomasKwa, tell me about it. The thing which really surprised me, in one of the versions I passed through on the way to the first one I posted, was that -1**x is always -1. I expected the -1 to be a single integer literal token rather than a low-precedence unary minus followed by a one. – Peter Taylor – 2015-11-30T22:21:38.993

2Congrats on the bounty! Your code is shorter than anything I had come up with. Your solution is based on a totally new and unexpected mathematical idea, and it brings me joy to that this is possible even in such a straightforward-looking challenge .The Mobius-inverse result is quite pretty and I took the pleasure of deriving a proof for myself. – xnor – 2015-12-05T08:45:56.157

1 byte can be saved by moving the multiplication by 4 to after **x. – feersum – 2015-12-05T09:05:57.533

@PeterTaylor Can you elaborate how your algorithm works/can you point me to a resource? I cannot quite see how you can apply the möbius inversion to number of sums of suqares problem. – flawr – 2015-12-05T12:55:11.030

@flawr, I haven't worked through the details, but (as mentioned in my CJam answer), the relationship is given in OEIS. MathWorld's discussion of the relevance of primes equal to 1 and 3 mod 4 probably provides enough ammunition to tackle the proof.

– Peter Taylor – 2015-12-05T14:40:07.973

@PeterTaylor Thank you very much! – flawr – 2015-12-05T16:45:49.553

If Python's boolean/number parsing works like JS's, +!n should work in place of 0**n. – ETHproductions – 2015-12-06T18:52:18.437

@ETHproductions, Python is quite idiosyncratic. It has and instead of &&, not instead of !, doesn't have ++ or ?:... – Peter Taylor – 2015-12-06T22:10:21.470

15

Mathematica, 13 bytes

If built-ins are allowed, this is how to do it in Mathematica.

2~SquaresR~#&

For 0 < = n <= 100

2~SquaresR~# & /@ Range[0, 100]

{1, 4, 4, 0, 4, 8, 0, 0, 4, 4, 8, 0, 0, 8, 0, 0, 4, 8, 4, 0, 8, 0, 0, 0, 0, 12, 8, 0, 0, 8, 0, 0, 4, 0, 8, 0, 4, 8, 0, 0, 8, 8, 0, 0, 0, 8, 0, 0, 0, 4, 12, 0, 8, 8, 0, 0, 0, 0, 8, 0, 0, 8, 0, 0, 4, 16, 0, 0, 8, 0, 0, 0, 4, 8, 8, 0, 0, 0, 0, 0, 8, 4, 8, 0, 0, 16, 0, 0, 0, 8, 8, 0, 0, 0, 0, 0, 0, 8, 4, 0, 12}

DavidC

Posted 2015-11-26T00:38:01.640

Reputation: 24 524

1Because of course Mathematica has a built-in for this. – HyperNeutrino – 2016-12-05T15:01:50.403

15

Python 2, 44 bytes

f=lambda n,x=1:+(x>n)or(n%x<1)-f(n,x+2)/4<<2

This is almost the same as xsot's solution (which is based on Peter Taylor's solution), but saves 8 bytes by simplifying the way signs are handled.

Note that for a full program, we can save 2 bytes in the function without incurring a cost outside the function:

f=lambda n,x=1:x>n or(n%x<1)-f(n,x+2)/4<<2
print+f(input())

Two additional bytes for a full program this way:

n=input()
f=lambda x:x>n or(n%x<1)-f(x+2)/4<<2
print+f(1)

For n > 0 there is a very legible 40-byte solution:

f=lambda n,x=1:n/x and(n%x<1)*4-f(n,x+2)

Mitch Schwartz

Posted 2015-11-26T00:38:01.640

Reputation: 4 899

1Congrats for winning the bounty! The recursive subtraction is a clean and short way to express the alternating sum for odd divisors without needing to extract a sign from the divisor itself. Also, credit to xsot for streamlining Peter Taylor's solution to a recursive one with clever handling of n=0. – xnor – 2015-12-13T07:33:03.897

12

J, 16 bytes

+/@,@:=+&*:/~@i:

This is a monadic verb (in other words, a unary function). Try it online or see it pass all test cases.

Explanation

+/@,@:=+&*:/~@i:  Denote input by n
              i:  The array of integers from -n to n
           /~@    Take outer product wrt the following function:
       +           the sum of
        &*:        squares of both inputs
                  This results in a 2D array of a^2+b^2 for all a, b between -n and n
      =           Replace by 1 those entries that are equal to n, and others by 0
   ,@:            Flatten the binary matrix
+/@               Take its sum

Zgarb

Posted 2015-11-26T00:38:01.640

Reputation: 39 083

12

Pyth, 13 bytes

/sM^^R2}_QQ2Q

Test suite

/sM^^R2}_QQ2Q
                 Q = eval(input())
       }_QQ      Inclusive range from -Q to Q (all possible a and b)
    ^R2          Map to their squares
   ^       2     Form all pairs
 sM              Sum pairs
/           Q    Count occurances of Q

isaacg

Posted 2015-11-26T00:38:01.640

Reputation: 39 268

Late, but I don't think you need the last Q. – Erik the Outgolfer – 2016-12-05T13:07:39.220

11

Python 2, 69 55 53 52 bytes

f=lambda n,x=1:+(x>n)or(2-x%4)*(n%x<1)+f(n,x+2)/4<<2

This is a recursive function based off Peter Taylor's excellent solution.

xsot

Posted 2015-11-26T00:38:01.640

Reputation: 5 069

1This is a great improvement. But, there is still a way to make it shorter, and I encourage you to look for it. – xnor – 2015-12-08T22:29:12.193

1@xnor Another byte down. I hope you don't have any more tricks up your sleeves. – xsot – 2015-12-09T11:32:39.257

Whoa, that operator precedence. I don't have this trick but I have a different, bigger one. – xnor – 2015-12-09T14:23:28.563

2I don't know whether I should make an answer of it, it's just your solution plus one trick: f=lambda n,x=1:+(x>n)or(n%x<1)-f(n,x+2)/4<<2. Also, I guess we don't care about exceeding the default maximum recursion depth? – Mitch Schwartz – 2015-12-09T20:38:30.580

(For positive n, can simplify f=lambda n,x=1:n/x and(n%x<1)*4-f(n,x+2)) – Mitch Schwartz – 2015-12-09T22:03:41.620

@xnor Is that the same idea as yours? – Mitch Schwartz – 2015-12-09T22:55:54.737

1@MitchSchwartz I think that's an incredible improvement worthy of the bounty and likely the final optimisation xnor had in mind. – xsot – 2015-12-09T23:07:06.617

1@MitchSchwartz Yes, that's the optimization I was thinking of! And xsot's /4<<2 trick makes it shorter than what I had. – xnor – 2015-12-09T23:30:05.470

8

Julia, 40 bytes

n->n>0?4sum(i->(n-i^2)^.5%1==0,1:n^.5):1

Ungolfed:

function f(n)
  if n==0
    return 1           # Handle special case of n=0
  else
    m=0                # Start the counter at zero
    for i=1:sqrt(n)    # Loop over the values (i) whose squares are
                       # less than n (except zero)
      k=sqrt(n-i^2)    # Find k such that n=k^2+i^2
      if k==floor(k)   # if k is an integer, we've found a pair
        m+=4           # Add one for each of k^2+i^2, (-k)^2+(-i)^2, and the other two
      end
    end
    return m           # Return the resulting count
  end
end

Note that the loop doesn't include i==0, because when n is a square, it's already included by i=sqrt(n), and there are only four, not eight, for that form (0^2+k^2, 0^2+(-k)^2, k^2+0^2, (-k)^2+0^2).

Glen O

Posted 2015-11-26T00:38:01.640

Reputation: 2 548

7

CJam, 25 23 bytes

Zri:R#Ym*{Rf-Yf#:+R=},,

This is a theoretical solution that requires O(9n) time and memory for input n.

At the cost of one extra byte – for a total of 24 bytes – we can reduce the complexity to O(n2):

ri:R)Y*Ym*{Rf-Yf#:+R=},,

Try it online!

How it works

Either

Z                  Push 3.
 ri:R              Read an integer from STDIN and save it in R.
     #             Compute 3**R.

or

ri:R               Read an integer from STDIN and save it in R.
    )Y*            Add 1 and multiply by 2.

Then

Ym*                Take the second Cartesian power, i.e., compute all pairs.
   {          },   Filter the pairs:
    Rf-              Subtract R from each.
       Yf#           Square the differences.
          :+         Add the squares.
            R=       Compare with R.
                   If = pushed 1, keep the pair.
                ,  Count the kept pairs.

Dennis

Posted 2015-11-26T00:38:01.640

Reputation: 196 637

And at a saving of one byte it's possible to get the complexity down to Õ(n) – Peter Taylor – 2015-11-30T14:18:33.037

Yes, I saw. That's amazing. – Dennis – 2015-11-30T14:20:05.813

7

CJam (25 24 22 21 bytes)

{:X!X{X\2*)%!4*\-}/z}

Online demo

This runs in pseudoquasilinear time* and uses the statement from the OEIS that

Moebius transform is period 4 sequence [ 4, 0, -4, 0, ...]. - Michael Somos, Sep 17 2007

The input 0 is obviously a special case (Möbius tranforms and annihilators don't go well together), but ended up only costing one char.

* Pseudo- because it's quasilinear in the value of the input, not the size of the input; quasi because it does Theta(n) operations on integers of size on the order of n; a b-bit mod operation should take b lg b time, so overall this takes Theta(n lg n lg lg n) time.

Peter Taylor

Posted 2015-11-26T00:38:01.640

Reputation: 41 901

6

Japt, 42 37 33 bytes

Japt is a shortened version of JavaScript. Interpreter

V=Un oU+1;Vr@X+(Vf_p2 +Y*Y¥U)l ,0

How it works

           // Implicit: U = input number
V=Un oU+1  // Set variable V to range(-U, U+1). Ends up like [-U,-U+1,...,U-1,U]
Vr@    ,0  // Reduce each item Y in V with this function, starting at 0:
X+(     l  //  Return the previous value X + the length of:
Vf_p2      //   V filtered by items Z where Z*Z
+Y*Y==U)   //    + Y*Y equals U.
           // This ends up as the combined length of all fitting pairs of squares.
           // Implicit: return last expression

Perhaps there's a better technique; suggestions are welcome.

ETHproductions

Posted 2015-11-26T00:38:01.640

Reputation: 47 880

6

Python 3, 68 61 60 bytes

lambda n:0**n+4*sum(i**.5%1+(n-i)**.5%1==0for i in range(n))

Using two nested list comprehensions is too expensive. Instead, this checks if both coordinates on the circle of radius sqrt(n) are integers.

Peter Taylor has beaten this with a Moebius-inversion based approach.

lirtosiast

Posted 2015-11-26T00:38:01.640

Reputation: 20 331

Well done. I was tinkering with a recursive function but couldn't resolve n=0 elegantly. – xsot – 2015-11-30T01:48:00.053

5

Julia, 89 79 63 bytes

g(x)=cos(π*x^.5)^2÷1
a(n)=(n==0)+4sum([g(i)g(n-i)for i=1:n])

This is a named function a which accepts an integer and returns a float. It calls a helper function g.

Ungolfed:

function g(x::Integer)
    floor(cos(π*sqrt(x))^2)
end

function a(n::Integer)
    (n == 0) + 4*sum([g(i)*g(n-i) for i=1:n])
end

The approach here uses a simplification of Wesley Ivan Hurt's formula listed on OEIS. The simplification was found by Glen O, the very same person who shaved 26 bytes off of this answer!

Alex A.

Posted 2015-11-26T00:38:01.640

Reputation: 23 761

Use x^.5 rather than sqrt(x) to save 3 bytes. And (n==0) saves 2 bytes over 1÷(n+1). And you can save 4 more characters by using cos(π*sqrt(x))^2÷1 rather than floor(cos(π*sqrt(x))^2). Also, use 1:n/2 rather than 1:n÷2, because there's no harm using a float in g(x) and it'll be locked to the integers for i anyway. And sum(i->g(i)g(n-i),1:n/2) will shave some more characters, too. – Glen O – 2015-11-26T03:12:30.290

@GlenO Great suggestions, thanks. The sum trick fails for n=0 though, so I kept array comprehension. – Alex A. – 2015-11-26T03:45:12.677

1So, it can be recovered - if you let the i=0 case happen in the sum, you can switch the sign on 4g(n). So (n==0)-4g(n)-4g(n/2)+8sum(i->g(i)g(n-i),0:n/2), which will not run into the error. But you can do even better, by noting the symmetries - (n==0)+4sum([g(i)g(n-i)for i=1:n]) – Glen O – 2015-11-27T03:11:18.840

@GlenO That simplification is seriously genius. I recommend you submit that as an alternative formula for the sequence on OEIS! – Alex A. – 2015-11-27T18:44:37.437

5

Octave, 28 bytes

@(n)nnz((a=(-n:n).^2)'+a==n)

alephalpha

Posted 2015-11-26T00:38:01.640

Reputation: 23 988

5

Haskell, 42 bytes

f n|q<-[-n..n]=sum[1|a<-q,b<-q,a*a+b*b==n]

Usage exapmle:

*Main> map f [0..25]
[1,4,4,0,4,8,0,0,4,4,8,0,0,8,0,0,4,8,4,0,8,0,0,0,0,12]
*Main> 

nimi

Posted 2015-11-26T00:38:01.640

Reputation: 34 639

3Binding q in a guard is clever, I'll remember this trick. – xnor – 2015-11-26T11:26:04.823

4

Pyth, 16 15 bytes

lfqQs^R2T^}_QQ2

Try it online in the Pyth Compiler.

How it works

lfqQs^R2T^}_QQ2

          }_QQ   Compute the inclusive range from -Q to Q (input).
         ^    2  Take the second Cartesian power, i.e., compute all pairs.
 f               Filter; for each T in the list of pairs:
     ^R2T          Compute the squares of T's elements.
    s              Add the squares.
  qQ               Compare the sum with Q.
                 If q returned True, keep T.
l                Count the kept pairs.

Dennis

Posted 2015-11-26T00:38:01.640

Reputation: 196 637

4

TI-BASIC, 23 bytes

sum(seq(Σ(X²+Y²=Ans,X,-Ans,Ans),Y,-Ans,Ans

Pretty straightforward. Σ( is summation.

Strangely, sum(seq(sum(seq( throws an ERR:ILLEGAL NEST, and so does Σ(Σ(, but sum(seq(Σ( is fine. I chose to put the Σ( on the inside to save a close-paren.

lirtosiast

Posted 2015-11-26T00:38:01.640

Reputation: 20 331

What's the difference between sum and Σ? – alephalpha – 2015-11-26T09:28:15.357

1@alephalpha Σ( takes a summation, adding up all of the X²+Y²=Ans from values of X between -Ans and Ans. sum( is sum of a list, so we need to create the list first using seq(...,Y,-Ans,Ans – lirtosiast – 2015-11-26T18:11:25.580

4

JavaScript (ES6), 66 60 bytes

n=>eval("for(r=0,a=~n;a++<n;)for(b=~n;b++<n;)r+=a*a+b*b==n")

6 bytes saved thanks to @edc65!

Explanation

n=>eval(`              // use eval to allow for loops in an unparenthesised arrow function
  for(r=0,             // r = number of pairs
    a=~n;a++<n;        // a = first number to square
  )
      for(b=~n;b++<n;) // b = second number to square
        r+=a*a+b*b==n  // add one to the result if a^2 + b^2 == n
                       // implicit: return r
`)

Test

n = <input type="number" oninput='result.innerHTML=(

n=>eval("for(r=0,a=~n;a++<n;)for(b=~n;b++<n;)r+=a*a+b*b==n")

)(+this.value)' /><pre id="result"></pre>

user81655

Posted 2015-11-26T00:38:01.640

Reputation: 10 181

160: n=>eval('for(r=0,a=~n;a++<n;)for(b=~n;b++<n;)r+=a*a+b*b==n') – edc65 – 2015-11-26T12:52:21.350

@edc65 Nice! I didn't think of using eval to put the for loops into an arrow function without parentheses. I also forgot about the ~ operator haha. – user81655 – 2015-11-26T14:20:29.090

4

Python 3, 93 62 69 bytes

Itertools wasn't working so I used two ranges again, but moved the range out to save bytes.

Edit: Previous code didn't actually work, since I defined range over n before I defined n.

lambda n:sum(i*i+j*j==n for i in range(-n,n+1)for j in range(-n,n+1))

Sherlock9

Posted 2015-11-26T00:38:01.640

Reputation: 11 664

2

APL, 23 20 19 bytes

{+/⍵=∊∘.+⍨×⍨0,,⍨⍳⍵}

Explanation:

{+/⍵=∊∘.+⍨×⍨0,,⍨⍳⍵}        Monadic function:
                 ⍳⍵          1 2 3 ... ⍵
               ,⍨            Duplicate
             0,              Concatenate to 0
          ×⍨                 Square everything
      ∘.+⍨                   Make an addition table
     ∊                       Flatten
   ⍵=                        1s where equal to the input
 +/                          Sum up the 1s

Other than the fact that APL doesn't have J's i: (numbers from -n to n) function, this works pretty much like the J answer.

We can't use a train because getting the -\⍳2×⍵ to not parse as (-\) ⍳ (2×⍵) would cost three bytes; similarly with other pair of atops. All those parentheses make the regular function shorter.

Try it here. The output of 1 means all values match.

lirtosiast

Posted 2015-11-26T00:38:01.640

Reputation: 20 331

2

PARI/GP, 34 28 bytes

Using generating functions:

Saved 6 bytes thanks to Mitch Schwartz.

n->sum(i=-n,n,x^i^2)^2\x^n%x

Using built-ins, 33 bytes (saved 1 byte thanks to Mitch Schwartz.):

n->if(n,2*qfrep(matid(2),n)[n],1)

qfrep(q,B,{flag=0}): vector of (half) the number of vectors of norms from 1 to B for the integral and definite quadratic form q. If flag is 1, count vectors of even norm from 1 to 2B.


alephalpha

Posted 2015-11-26T00:38:01.640

Reputation: 23 988

matid(2) saves a byte. – Mitch Schwartz – 2015-12-09T22:39:05.283

1And down to 28 for the generating function approach: n->sum(i=-n,n,x^i^2)^2\x^n%x – Mitch Schwartz – 2015-12-09T22:48:02.090

2

Matlab 41 bytes

Even smaller as the previous answers

@(n)nnz(~mod(sqrt(n-(1:n^.5).^2),1))*4+~n

Essentially Agawa001's answer with power and sqrt replaced

Jonas

Posted 2015-11-26T00:38:01.640

Reputation: 177

2

Candy, 17 14 bytes

Input pushed onto stack initially

~TbAT1C(sWs+Aeh)Z

~T0C(sWs+Aeh)Z

peekA    # copy arg from stack to register A
range2   # create double sided range on stack, -A, 1-A, ... A-1, A
digit0   # prefix argument to 'cart', 
cart     # cartesian product of current stack(0), and other stack(0)
while    # while stack not empty
  sqr    # pop and square and push
  swap   # swap two stack elements
  sqr    # pop and square and push
  add    # pop and pop and add and push
  pushA  # push original argument
  equal  # equality test 0/1
  popAddZ  # Z := Z + pop
endwhile
pushZ    # push Z onto stack, will be output to stdout on termination

Dale Johnson

Posted 2015-11-26T00:38:01.640

Reputation: 509

2

CJam, 28

qi_mF{3a>},{~)\4%2-&}%4+:*1?

Not really short, but efficient. E.g. the result for 15625 is instantly 28. Uses the factorization-based formula from OEIS.
Try it online

Explanation:

qi       read input and convert to integer
_        make a copy (will be used to handle the 0 case at the end)
mF       factorize into [prime exponent] pairs
{…},     filter the array of pairs
  3a>    with the condition that the pair is greater than [3]
          which means the prime factor must be ⩾3
{…}%     transform each pair as follows:
  ~      dump the prime factor and exponent onto the stack
  )      increment the exponent
  \      swap with the prime
  4%     get the remainder mod 4 (it will be 1 or 3)
  2-     subtract 2 (resulting in -1 or 1)
  &      bitwise AND with the incremented exponent (see below)
4+       append a 4 to the array
:*       multiply all
1?       if the input was 0, use 1, else use the above result

Some details about the calculation:

  • if the prime is 1 mod 4, the code calculates (exponent + 1) & -1, which is exponent + 1
  • if the prime is 3 mod 4, the code calculates (exponent + 1) & 1, which is 0 if the exponent is odd, and 1 if even

All these values multiplied together and multiplied by 4 are exactly the OEIS formula.

aditsu quit because SE is EVIL

Posted 2015-11-26T00:38:01.640

Reputation: 22 326

2

Python 2, 68 bytes

def x(n):r=range(-n,n+1);print sum(a*a+b*b==n for a in r for b in r)

Defines a function called x() that takes a number n.

Try it online. http://ideone.com/aRoxGF

Rɪᴋᴇʀ

Posted 2015-11-26T00:38:01.640

Reputation: 7 410

You are missing a print or return statement. – Zgarb – 2015-11-28T23:47:29.607

Thanks, I completely forgot. The link has the print statement though. I edited my code while I was making the code. – Rɪᴋᴇʀ – 2015-11-28T23:51:14.953

Ok, no worries. But this also seems to give incorrect results for n=0 and n=1 (0 and 2 instead of 1 and 4). Maybe the range limits need adjusting? – Zgarb – 2015-11-28T23:56:00.447

@Zgarb Yeah, they should end at n+1. – lirtosiast – 2015-11-28T23:56:53.407

This looks pretty short, but knowing xnor I don't think it'll be this simple. – lirtosiast – 2015-11-28T23:57:44.237

I tested it (the new version) for all of the values he gave, that is 0-25. They were all correct. I haven't tested it for anything else though. – Rɪᴋᴇʀ – 2015-11-29T00:01:22.650

I've tested a version of this, slightly modified to run on Python 3, from 0 to 101 (values provided by the OEIS). It works for all of them. – Sherlock9 – 2015-11-29T00:52:04.697

@RikerW I wasn't questioning the validity; I was saying xnor's bounty means there's a shorter but hard-to-find solution. – lirtosiast – 2015-11-29T01:20:44.707

1I'll look for it. – Rɪᴋᴇʀ – 2015-11-29T01:23:53.780

2

Pyth, 41 35 33 30 27 bytes

Try it online.

Edit: Thanks to isaacg, I got m and *F to work! YES!

?Q*F+4m.&tt%ed4hhdr-PQ2 8 1
                                (implicit) Q = input()
?Q                              If Q != 0
      m                           Map to d (exponent, prime) from ...
                  r-PQ2 8         run-length-encoded(PQ with 2's removed)
       .&                           Bitwise and
           %ed4                       d[-1] % 4
         tt                           -2
                hd                  with d[0]
               h                      +1
    +4                            Append 4 to the resulting array
  *F                              Then multiply it all together
                          1     Else 1

Edit: Put the bitwise and back in for more byte savings! Also I removed all the "Formerly" stuff. It was starting to get cluttered.

Thanks to aditsu and his CJam solution, and to maltysen and his tips (One day I will get m*Fd to work. One day...)

J4Vr-PQ2 8=J*J.&tt%eN4hhN;?QJ1
                                (implicit) Q=input()
J4                              J=4
    -PQ2                        Remove all 2's from the prime factorization of Q
   r     8                      run-length encode (exponent, prime factor)
  V                      ;      For N in range( the above ):
          =J*J                      J = J * ...
                tt%eN4                N[-1] mod 4 -2 
                      hhN             (exponent + 1)
              .&                    bitwise and
                          ?QJ1  if Q != 0 print(J) else print(1)

Note that,

  • if the prime is 1 mod 4, we get -1 & (exponent + 1), which is exponent + 1

  • but if the prime is 3 mod 4, we get 1 & (exponent + 1), which is 0 if the exponent is odd, and 1 if even

Multiply it all together (times 4 at the beginning) and we get the number of sums of two squares that add up to our input.

Sherlock9

Posted 2015-11-26T00:38:01.640

Reputation: 11 664

2

Python, 57 bytes

Nice challenge. Unfortunately I'm not getting it shorter than this at the moment.

lambda n:0**n+sum(2-d%4for d in range(1,n+1)if d%2>n%d)*4

Willem

Posted 2015-11-26T00:38:01.640

Reputation: 1 528

1

ASP, 53 + 4 = 57 bytes

#show N:N=#count{o(A,B):k=A**2+B**2,A=-k..k,B=-k..k}.

Answer Set Programming is a logical language, similar to prolog. I use here the Potassco implementation, clingo.

Input is taken from parameters (-ck= is 4 bytes long). Call example:

clingo -ck=25

Output sample:

12

You can try it in your browser ; unfortunately, this method doesn't handle call flags, so you need to add the line #const k=25 in order to make it work.

aluriak

Posted 2015-11-26T00:38:01.640

Reputation: 141

1

Add++, 31 bytes

D,f,@,.5^1+iR2€Ω^d0BFB]d‽+A€=¦+

Try it online!

How it works

This defines a function, \$f\$, that takes the input, \$x\$, as an argument and returns the correct output. We start by yielding \$y := \lfloor\sqrt{x}+1\rfloor\$. We then push the range \$a := [1, 4, ..., y^2]\$, the list of square numbers up to the smallest square number larger than \$x\$. We then duplicate this array and push \$0\$ to the stack. At this point, the stack looks like this, for an input of \$25\$:

[[1 4 9 16 25 36] [1 4 9 16 25 36] 0]

We then collect these values into a single list, which yields the list of \$n^2\$ for each \$n \in [-y, y]\$. We then duplicate this list and operate the table operator over the addition command. The table operator takes a dyad, \$g(p, q)\$, and two arrays, \$A\$ and \$B\$. It then takes the Cartesian Product of \$A\$ and \$B\$ and iterates \$g(a, b)\$ over each pair \$(a, b)\$ where \$a \in A\$ and \$b \in B\$.

In this code, this yields the array \$\big[a^2+b^2 \: | \: a, b \in [-y, y]\big]\$. We then compare each element of this list with the input, yielding a boolean array. Finally, we count the number of \$1\$s in this array by taking its sum, and returning that total.

Most of the code should be understandable when paired with the explanation. A few of the overlooked commands:

  • Ω : The reverse operator. Takes a dyad and reverses the order of the arguments
  • : The table operator, as described above.
  • ¦ : The fold operator. Takes an array and a dyad and reduces by the dyad. ¦+ is an alias for sum.

user81402

Posted 2015-11-26T00:38:01.640

Reputation:

1

MathGolf, 9 bytes

╤■mæ²Σk=Σ

Try it online.

Explanation:

╤         # Take the (implicit) input-integer, and push a list in the range [-input, input]
 ■        # Take the cartesian product of this, creating a list of all possible pairs
  mæ      # Map these pairs to, using the following four commands:
    ²     #  Take the square of both values in the pair
     Σ    #  Sum those
      k=  #  And check whether it's equal to the input-integer (1 if truthy; 0 if falsey)
  Σ       # After the map, sum the list
          # (after which the entire stack joined together is output implicitly)

Kevin Cruijssen

Posted 2015-11-26T00:38:01.640

Reputation: 67 575

1

05AB1E, 7 bytes

(ŸãnOQO

Try it online or verify the test cases in the range \$[0,100]\$.

Explanation:

(        # Get the negative of the (implicit) input-integer
 Ÿ       # Push a list in the range [(implicit) input-integer, -input]
  ã      # Get the cartesian product of this list, creating all possible pairs
   n     # Square each value in each pair
    O    # Sum each inner pair
     Q   # Check for each sum whether it's equal to the (implicit) input-integer
         # (1 if truthy; 0 if falsey)
      O  # And sum those
         # (after which the result is output implicitly)

Kevin Cruijssen

Posted 2015-11-26T00:38:01.640

Reputation: 67 575

1

Matlab, 72 bytes

n=input('');m=fix(sqrt(n));m=(-m:m).^2;disp(nnz(bsxfun(@plus,m,m')==n))

Luis Mendo

Posted 2015-11-26T00:38:01.640

Reputation: 87 464

You don't need disp in this challenge Luis! =)

– Stewie Griffin – 2015-11-26T12:21:57.467

@StewieGriffin Thanks! But in this case it's a program, not a function. So according to the accepted answer in your link it's needed, isn't it? – Luis Mendo – 2015-11-26T13:06:56.727

1

Matlab, 63 50 bytes

@(y)nnz(~mod(sqrt(y-power((1:sqrt(y)),2)),1))*4+~y

  • This does beat the other same-entitled code, thus I put it :D.

  • The program finds the positive integer solutions then multiply by 4 to encompass negative ones.

  • It can perform all 25 first test cases

    for i=1:25 ans(i)
    end
    
       1
    
       4
    
       4
    
       0
    
       4
    
       8
    
       0
    
       0
    
       4
    
       4
    
       8
    
       0
    
       0
    
       8
    
       0
    
       0
    
       4
    
       8
    
       4
    
       0
    
       8
    
       0
    
       0
    
       0
    
       0
    
       12
    

Abr001am

Posted 2015-11-26T00:38:01.640

Reputation: 862

You don't need disp in this challenge! =)

– Stewie Griffin – 2015-11-26T12:22:21.560

thanks @StewieGriffin i did include it just as a fair-game relating to luis' one – Abr001am – 2015-11-26T12:40:12.583

Tips: When you're planning on posting the results from MATLAB, use format compact =) – Stewie Griffin – 2015-11-27T06:57:50.860

1

JavaScript, 89 bytes

n=prompt()
p=Math.pow
for (x=c=(+n?0:1);x<=n;x++)if(x&&p(n-p(x,2),.5)%1===0)c+=4
alert(c)

I know this isn't the shortest JavaScript answer even if I were to remove the i/o lines, but I do think it is the best performing JS answer giving me the result for a million within a few seconds (ten million took about a minute).

Adam Dally

Posted 2015-11-26T00:38:01.640

Reputation: 331

Can you use == instead of ===? – lirtosiast – 2015-11-30T03:54:37.030

I could, just using best practices, ha ha. – Adam Dally – 2015-12-01T00:07:52.620

1

PHP, 80 Bytes

for($m=-$a=1+$argv[1];++$m<$a;)for($n=-$a;$n++<$a;)$c+=$a-1==$m**2+$n**2;echo$c;

Jörg Hülsermann

Posted 2015-11-26T00:38:01.640

Reputation: 13 026

1$c+=condition; instead of if(condition)$c++; (-4) Do you feel stalked? :D pre-increment on $m and $n will improve speed a bit. – Titus – 2016-10-31T00:49:01.823

1

PHP, 70 bytes, not competing

for($x=-1;$x++<=$n=$argv[1];)$s+=(-($n%($x-~$x)<1))**$x*4;echo$n?$s:1;

algorithm stolen from one of the Python answers ... I forgot which one; wanted to at least partially understand what´s happening before I posted.

Titus

Posted 2015-11-26T00:38:01.640

Reputation: 13 814

for(;$x<=$n=$argv[1];)$s+=(-($n%(2*$x+1)<1))**$x++*4;echo$n?$s:1; saves 5 Bytes. $x-~$x is equal to 2*$x+1 and you can now start without assigning the variable. – Jörg Hülsermann – 2016-10-31T12:35:46.260

0

Axiom 115 bytes

g(n)==(v:INT:=truncate(sqrt(n)::Float);c:=0;for i in -v..v repeat for j in -v..v repeat if i^2+j^2=n then c:=c+1;c)

ungolfed

gg(n:NNI):NNI== 
   v:NNI:=truncate(sqrt(n)::Float)
   c:NNI:=0
   for i in -v..v repeat
      for j in -v..v repeat
          if i^2+j^2=n then c:=c+1
   c

results

(5) -> [i,g(i)]  for i in 0..25
   Compiling function g with type NonNegativeInteger -> NonNegativeInteger
   (5)
   [[0,1], [1,4], [2,4], [3,0], [4,4], [5,8], [6,0], [7,0], [8,4], [9,4],
    [10,8], [11,0], [12,0], [13,8], [14,0], [15,0], [16,4], [17,8], [18,4],
    [19,0], [20,8], [21,0], [22,0], [23,0], [24,0], [25,12]]
                                      Type: Tuple List NonNegativeInteger

dobious results

(13) -> g(10018)
   (13)  8
                                                    Type: PositiveInteger
(14) -> g(10019)
   (14)  0
                                                 Type: NonNegativeInteger
(15) -> g(10020)
   (15)  0

RosLuP

Posted 2015-11-26T00:38:01.640

Reputation: 3 036

0

C++, 81 bytes

#define F(x) for(int x=~n;x++<n;)
void f(int n,int&r){r=0;F(a)F(b)r+=a*a+b*b==n;}

Returns via reference parameter. Simply two ranges over [-n,n], so it works for n=0.

Karl Napf

Posted 2015-11-26T00:38:01.640

Reputation: 4 131

0

Perl 5, 52 bytes

56 bytes:

$n=pop;for$a(@a=-$n..$n){map$i+=$_*$_+$a*$a==$n,@a}say$i

If output can be in base 1, then 52 bytes:

$n=pop;for$a(@a=-$n..$n){print$_*$_+$a*$a==$n for@a}

msh210

Posted 2015-11-26T00:38:01.640

Reputation: 3 094

Why waste so many bytes using an array? You can do the -$n..$n in the for statement... – Wouter Verhelst – 2015-11-26T09:13:19.087

@WouterVerheist I use @a twice, and wouldn't save bytes writing -$n..$n twice. However, presumably I can incorporate the assignment to @a into the first time it's used, which will save 3 bytes. If I have an opportunity to test it and remember to do so, I'll do so and revise this answer. – msh210 – 2015-11-26T14:56:17.653

Ah, yes, missed that. I must be blind. – Wouter Verhelst – 2015-11-26T14:58:46.033

@WouterVerhelst But thanks for the tip: you saved me three bytes. – msh210 – 2015-11-26T17:44:03.417

0

Python, 76 74 bytes

I'm sure this can be golfed more, but I need to get back to work.

lambda n:sum([1for a in range(-n,n+1)for b in range(-n,n+1)if a*a+b*b==n])

Try it online

Thanks to @mathmandan for taking off 2 bytes.

agtoever

Posted 2015-11-26T00:38:01.640

Reputation: 2 661

Quick note: a*a+b*b is shorter than a**2+b**2. Also the square brackets aren't required here, so you can get down to 72 bytes. (FYI, it looks like def f(n):r=range(-n,n+1);print sum(1for j in r for i in r if i*i+j*j==n) is also the exact same score.) – mathmandan – 2015-11-26T18:02:39.303

Thanks. The a*a is almost obvious - stupid I didn't see that. The brackets are needed for the comprehension to work. So it's down to 74. Also putting lambda n,r=range results in the same length. Too bad... – agtoever – 2015-11-27T08:02:07.633

1

Without the square brackets, it won't make a list, but it'll make a generator, which works fine with sum. https://docs.python.org/2/tutorial/classes.html#generator-expressions (Removed square brackets and tested fine in 2.7.4.)

– mathmandan – 2015-11-27T16:05:10.177

0

Ruby, 66 58 56 54 bytes

->n{r=-n..n;r.map{|x|r.count{|y|n==x*x+y*y}}.reduce:+}

Thanks to sherlock9.

56 bytes

->n{r=-n..n;r.map{|x|r.count{|y|n==x**2+y**2}}.reduce:+}

58 bytes

->n{c=0;r=(-n..n);r.map{|x|c+=r.count{|y|n==x**2+y**2}};c}

66 bytes

->n{c=0;(-n..n).each{|x|c+=n.downto(-n).count{|y|n==x**2+y**2}};c}

Ungolfed:

-> n {
  r = -n..n
  r.map { |x|
    r.count { |y|
      n == x*x + y*y
    }
  }.reduce:+
}

Usage:

->n{r=-n..n;r.map{|x|r.count{|y|n==x*x+y*y}}.reduce:+}[25]
=> 12

Vasu Adari

Posted 2015-11-26T00:38:01.640

Reputation: 941

You can shave two bytes by using x*x+y*y – Sherlock9 – 2015-11-29T01:12:05.747

0

Python, 133 129 bytes

from itertools import*
n=input()
l=[];s=0
for i in range(-n,n+1):l.append(i**2)
for j in product(l,l):
 if sum(j)==n:s+=1
print s

This demonstrates the use of itertools cartesian product. Try it online

TanMath

Posted 2015-11-26T00:38:01.640

Reputation: 1 431

@Mego I tried, I don't think there is anything to further golf. I will take that part out if you insist. – TanMath – 2015-11-28T21:01:41.030

0

C, 224 219 182 175 158 bytes

Non-golfed version (with descriptive variables):

#include <math.h>

main (argc, argv) 
    char **argv;
{

    int n, count, max, i, j;
    n = atoi (argv[1]);
    count = 0;
    max = sqrt ((float) n);

    for (i = -max; i <= max; i++)
        for (j = -max; j <= max; j++)
            if (i * i + j * j == n)
                count++;

    printf ("%d", count);
}

Golfed version (with short variables):

#include<math.h>
main(c,v)char**v;{int n,o,x,i,j;n=atoi(v[1]);o=0;x=sqrt((float)n);for(i=-x;i<=x;i++)for(j=-x;j<=x;j++)if(i*i+j*j==n)o++;printf("%d",o);}

Update Sorry about the multiple edits, I'm learning how to abuse K&R-style C ;)

tonysdg

Posted 2015-11-26T00:38:01.640

Reputation: 141

You can remove the sqrt and just set the forloop bounds to -n and +n. This calculates a lot more but is shorter ;) – Simon – 2015-12-05T19:28:10.383

0

C++, 175 bytes

#include<iostream> using namespace std; int main() {int i,j,k=0,n; cin>>n; for(i=1;i<n;++i) for(j=1;j<n;++j) if(i*i+j*j==n) ++k; for(i=1;i<=n;++i) if(i*i==n) ++k; cout<<4*k;}

Ungolfed

#include<iostream>
using namespace std;
int main()
{
int i,j,k=0,n;
cin>>n;
for(i=1;i<n;++i)
 for(j=1;j<n;++j)
  if(i*i+j*j==n)
   ++k;
for(i=1;i<=n;++i)
 if(i*i==n)
  ++k;
cout<<4*k;
}

ghosts_in_the_code

Posted 2015-11-26T00:38:01.640

Reputation: 2 907

I hope it's okay now. – ghosts_in_the_code – 2015-11-30T16:46:45.327

Not quite. 1. Given input of 0, it gives output of 0. That's a special case, and should be 1. 2. It doesn't compile as is. The #include needs a newline before the following statement. 3. Some tips: you can remove 11 chars just by cutting out unnecessary whitespace. You can remove more with some basic transformations like for(i=1;i<n;++i) being equivalent to for(i=0;++i<n;). If you change the range of the loop over j then I think you can eliminate the second loop over i entirely. – Peter Taylor – 2015-11-30T22:29:35.890

0

Swift 2.0, 110 108 107 bytes

var n=Int(readLine()!)!,c=0;for(var i = -n;i<=n;i++){for(var j = -n;j<=n;j++){if i*i+j*j==n{c++}}};print(c)

First attempt

Simon

Posted 2015-11-26T00:38:01.640

Reputation: 101

I think you could save many bytes by removing whitespace. – geokavel – 2015-12-06T03:35:10.603

I can only remove two but thanks! – Simon – 2015-12-06T13:14:42.713

What about when you create the variables? – geokavel – 2015-12-06T15:57:53.240

The online swift compiler gives errors in that cases, dont have a mac close atm – Simon – 2015-12-06T20:39:49.590