<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: The Sum Of Two Squares</title>
	<atom:link href="http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Sat, 11 Feb 2012 09:48:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Drake</title>
		<link>http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/#comment-4126</link>
		<dc:creator><![CDATA[Drake]]></dc:creator>
		<pubDate>Sat, 31 Dec 2011 00:40:35 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1856#comment-4126</guid>
		<description><![CDATA[At the end of the chapter Dijkstra includes this: &quot;Note. Obvious improvements, such as testing whether r mod 4 =3, and exploiting 
the recurrence relation (x+1)^2 = x^2 + (2x +1) are left as exercises.&quot;]]></description>
		<content:encoded><![CDATA[<p>At the end of the chapter Dijkstra includes this: &#8220;Note. Obvious improvements, such as testing whether r mod 4 =3, and exploiting<br />
the recurrence relation (x+1)^2 = x^2 + (2x +1) are left as exercises.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen Walcher</title>
		<link>http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/#comment-2285</link>
		<dc:creator><![CDATA[Stephen Walcher]]></dc:creator>
		<pubDate>Sat, 08 Jan 2011 20:31:49 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1856#comment-2285</guid>
		<description><![CDATA[Hey guys. Was looking at this code for an example. Ended up moving on but not before porting one of the functions over to PHP. Hope someone can use eventually.

[sourcecode lang=&quot;php&quot;]
&lt;?php
function squares($n) {
	$pairs = array();
	
	$x = sqrt($n);
	
	while ( $x-- &gt; sqrt($n / 2) ) {
		$y = sqrt($n - $x * $x);
		
		while ($x * $x + $y * $y &lt;= $n) {
			if ($y &gt; $x)
				break;
			if ($x * $x + $y * $y == $n)
				array_push($pairs, array($x, $y));
			++$y;
		}
	}
	
	echo sprintf(&quot;%d -- Pairs: %d\r\n&quot;, $n, count($pairs));
}

squares(50);
squares(48612265);
?&gt;
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Hey guys. Was looking at this code for an example. Ended up moving on but not before porting one of the functions over to PHP. Hope someone can use eventually.</p>
<pre class="brush: php;">
&lt;?php
function squares($n) {
	$pairs = array();

	$x = sqrt($n);

	while ( $x-- &gt; sqrt($n / 2) ) {
		$y = sqrt($n - $x * $x);

		while ($x * $x + $y * $y &lt;= $n) {
			if ($y &gt; $x)
				break;
			if ($x * $x + $y * $y == $n)
				array_push($pairs, array($x, $y));
			++$y;
		}
	}

	echo sprintf(&quot;%d -- Pairs: %d\r\n&quot;, $n, count($pairs));
}

squares(50);
squares(48612265);
?&gt;
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/#comment-1142</link>
		<dc:creator><![CDATA[Mike]]></dc:creator>
		<pubDate>Fri, 09 Apr 2010 02:45:55 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1856#comment-1142</guid>
		<description><![CDATA[Because x &gt;= y, decrementing x always causes a bigger change in the sum than incrementing y.  So the loop in the my routine above can be shortened to:

[sourcecode lang=&quot;python&quot;][/sourcecode]

def sumofsquares(n):
    &#039;&#039;&#039;Generate (x,y) pairs, where x*x + y*y == n and x &gt;= y &gt;= 0.

    Find max x, such that x*x &lt;= n.  Then loop, comparing y*y + x*x to n.
    If the sum is too small, increment y; if too big, decrement x.

    &quot;sos&quot; is the sum of the squares.
    &#039;&#039;&#039;

    x, y, sos = 0, 0, 0
    while sos = y:
        while sos &lt; n:
            sos += 2*y + 1
            y += 1

        if sos == n:
            yield x,y
            
        sos -= 2*x - 1
        x -= 1]]></description>
		<content:encoded><![CDATA[<p>Because x &gt;= y, decrementing x always causes a bigger change in the sum than incrementing y.  So the loop in the my routine above can be shortened to:</p>
<p>def sumofsquares(n):<br />
    &#8221;&#8217;Generate (x,y) pairs, where x*x + y*y == n and x &gt;= y &gt;= 0.</p>
<p>    Find max x, such that x*x &lt;= n.  Then loop, comparing y*y + x*x to n.<br />
    If the sum is too small, increment y; if too big, decrement x.</p>
<p>    &quot;sos&quot; is the sum of the squares.<br />
    &#039;&#039;&#039;</p>
<p>    x, y, sos = 0, 0, 0<br />
    while sos = y:<br />
        while sos &lt; n:<br />
            sos += 2*y + 1<br />
            y += 1</p>
<p>        if sos == n:<br />
            yield x,y</p>
<p>        sos -= 2*x &#8211; 1<br />
        x -= 1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/#comment-1141</link>
		<dc:creator><![CDATA[Mike]]></dc:creator>
		<pubDate>Fri, 09 Apr 2010 01:07:05 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1856#comment-1141</guid>
		<description><![CDATA[Here is a solution that only uses adds and multiply by 2 (could use a shift).

[sourcecode lang=&quot;python&quot;]
def sumofsquares(n):
    &#039;&#039;&#039;Generate (x,y) pairs, where x*x + y*y == n and x &gt;= y &gt;= 0.

    Find max x, such that x*x &lt;= n.  Then loop, comparing y*y + x*x to n.
    If the sum is too small, increment y; if too big, decrement x.
    &#039;&#039;&#039;
    x, y, sos = 0, 0, 0
    while sos &lt; n:
        sos += 2*x + 1
        x += 1


    while x &gt;= y:
        if sos &lt; n:
            sos += 2*y + 1
            y += 1

        elif sos &gt; n:
            sos -= 2*x - 1
            x -= 1

        else:
            yield x,y
            sos += 2*( y -x + 1 )
            y += 1
            x -= 1
            
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Here is a solution that only uses adds and multiply by 2 (could use a shift).</p>
<pre class="brush: python;">
def sumofsquares(n):
    '''Generate (x,y) pairs, where x*x + y*y == n and x &gt;= y &gt;= 0.

    Find max x, such that x*x &lt;= n.  Then loop, comparing y*y + x*x to n.
    If the sum is too small, increment y; if too big, decrement x.
    '''
    x, y, sos = 0, 0, 0
    while sos &lt; n:
        sos += 2*x + 1
        x += 1

    while x &gt;= y:
        if sos &lt; n:
            sos += 2*y + 1
            y += 1

        elif sos &gt; n:
            sos -= 2*x - 1
            x -= 1

        else:
            yield x,y
            sos += 2*( y -x + 1 )
            y += 1
            x -= 1
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jebb</title>
		<link>http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/#comment-957</link>
		<dc:creator><![CDATA[Jebb]]></dc:creator>
		<pubDate>Sun, 24 Jan 2010 17:11:33 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1856#comment-957</guid>
		<description><![CDATA[I&#039;ve tried to pay extra attention to the boundaries of both while loops, to avoid any unnecessary iteration. I&#039;m not convinced it&#039;s optimal, though: I don&#039;t particularly like the &quot;if (y &gt; x) break;&quot;.
[sourcecode lang=&quot;cpp&quot;]
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;

int main()
{
    unsigned int n, x, y;
    printf(&quot;Enter the integer n:\n&quot;);
    scanf(&quot;%d&quot;, &amp;n);
    x = sqrt(n);
    while (x-- &gt; sqrt(n / 2)) {
        y = sqrt(n - x * x); 
        while (x * x + y * y &lt;= n) {
            if (y &gt; x)
                break;
            if (x * x + y * y == n)
                printf(&quot;%d %d\n&quot;, x, y); 
            ++y;
        }   
    }   
    return 0;
}
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>I&#8217;ve tried to pay extra attention to the boundaries of both while loops, to avoid any unnecessary iteration. I&#8217;m not convinced it&#8217;s optimal, though: I don&#8217;t particularly like the &#8220;if (y &gt; x) break;&#8221;.</p>
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;

int main()
{
    unsigned int n, x, y;
    printf(&quot;Enter the integer n:\n&quot;);
    scanf(&quot;%d&quot;, &amp;n);
    x = sqrt(n);
    while (x-- &gt; sqrt(n / 2)) {
        y = sqrt(n - x * x);
        while (x * x + y * y &lt;= n) {
            if (y &gt; x)
                break;
            if (x * x + y * y == n)
                printf(&quot;%d %d\n&quot;, x, y);
            ++y;
        }
    }
    return 0;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jamie Hope</title>
		<link>http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/#comment-889</link>
		<dc:creator><![CDATA[Jamie Hope]]></dc:creator>
		<pubDate>Fri, 08 Jan 2010 16:40:31 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1856#comment-889</guid>
		<description><![CDATA[Oh, I see. It should be &quot;language&quot; and not &quot;lang&quot;. But the HOWTO page says &quot;lang&quot;:
http://programmingpraxis.com/contents/howto-posting-source-code/

Please fix, as I&#039;ll probably forget this by the time I go to post source code again. Thanks!]]></description>
		<content:encoded><![CDATA[<p>Oh, I see. It should be &#8220;language&#8221; and not &#8220;lang&#8221;. But the HOWTO page says &#8220;lang&#8221;:<br />
<a href="http://programmingpraxis.com/contents/howto-posting-source-code/" rel="nofollow">http://programmingpraxis.com/contents/howto-posting-source-code/</a></p>
<p>Please fix, as I&#8217;ll probably forget this by the time I go to post source code again. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin</title>
		<link>http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/#comment-886</link>
		<dc:creator><![CDATA[Kevin]]></dc:creator>
		<pubDate>Fri, 08 Jan 2010 07:21:44 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1856#comment-886</guid>
		<description><![CDATA[OH CRAP I&#039;m so sorry for being a noob. I JUST read the &quot;Posting Source Code&quot; page. So sorry!!

[sourcecode language=&quot;cpp&quot;]
#include&lt;iostream&gt;
#include&lt;cmath&gt;
using namespace std;
int main(void)
{
	int n, i=0;
	cout &lt;&lt; &quot;Integer? &quot;;
	cin &gt;&gt; n;
	double temp_frac, a, b;
	do
	{
		a = pow(i,2);
		b = sqrt(n-a);
		if( a &gt; n )
			break;
		else if( i &gt; b )
			break;
		else if( modf(b, &amp;temp_frac) == 0 )
			cout &lt;&lt; i &lt;&lt; &quot; , &quot; &lt;&lt; b &lt;&lt; endl;
		++i;
	}
	while(1);
	return 0;
}
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>OH CRAP I&#8217;m so sorry for being a noob. I JUST read the &#8220;Posting Source Code&#8221; page. So sorry!!</p>
<pre class="brush: cpp;">
#include&lt;iostream&gt;
#include&lt;cmath&gt;
using namespace std;
int main(void)
{
	int n, i=0;
	cout &lt;&lt; &quot;Integer? &quot;;
	cin &gt;&gt; n;
	double temp_frac, a, b;
	do
	{
		a = pow(i,2);
		b = sqrt(n-a);
		if( a &gt; n )
			break;
		else if( i &gt; b )
			break;
		else if( modf(b, &amp;temp_frac) == 0 )
			cout &lt;&lt; i &lt;&lt; &quot; , &quot; &lt;&lt; b &lt;&lt; endl;
		++i;
	}
	while(1);
	return 0;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin</title>
		<link>http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/#comment-885</link>
		<dc:creator><![CDATA[Kevin]]></dc:creator>
		<pubDate>Fri, 08 Jan 2010 07:17:48 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1856#comment-885</guid>
		<description><![CDATA[Here&#039;s mine in C++...it&#039;s somewhat bruteforce. If there are no solutions it wont give any output. Please, gimme some feedback! I&#039;m a student that&#039;s trying to practice/learn and get a better feel of C++. I wanna make it my goto language :D

#include
#include
using namespace std;
int main(void)
{
	int n, i=0;
	cout &lt;&gt; n;
	double temp_frac, a, b;
	do
	{
		a = pow(i,2);
		b = sqrt(n-a);
		if( a &gt; n )
			break;
		else if( i &gt; b )
			break;
		else if( modf(b, &amp;temp_frac) == 0 )
			cout &lt;&lt; i &lt;&lt; &quot; , &quot; &lt;&lt; b &lt;&lt; endl;
		++i;
	}
	while(1);
	return 0;
}]]></description>
		<content:encoded><![CDATA[<p>Here&#8217;s mine in C++&#8230;it&#8217;s somewhat bruteforce. If there are no solutions it wont give any output. Please, gimme some feedback! I&#8217;m a student that&#8217;s trying to practice/learn and get a better feel of C++. I wanna make it my goto language :D</p>
<p>#include<br />
#include<br />
using namespace std;<br />
int main(void)<br />
{<br />
	int n, i=0;<br />
	cout &lt;&gt; n;<br />
	double temp_frac, a, b;<br />
	do<br />
	{<br />
		a = pow(i,2);<br />
		b = sqrt(n-a);<br />
		if( a &gt; n )<br />
			break;<br />
		else if( i &gt; b )<br />
			break;<br />
		else if( modf(b, &amp;temp_frac) == 0 )<br />
			cout &lt;&lt; i &lt;&lt; &quot; , &quot; &lt;&lt; b &lt;&lt; endl;<br />
		++i;<br />
	}<br />
	while(1);<br />
	return 0;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: programmingpraxis</title>
		<link>http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/#comment-883</link>
		<dc:creator><![CDATA[programmingpraxis]]></dc:creator>
		<pubDate>Thu, 07 Jan 2010 23:42:00 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1856#comment-883</guid>
		<description><![CDATA[Andrew, Jaime: I fixed your comments so the source code is properly formatted.]]></description>
		<content:encoded><![CDATA[<p>Andrew, Jaime: I fixed your comments so the source code is properly formatted.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Humphreys</title>
		<link>http://programmingpraxis.com/2010/01/05/the-sum-of-two-squares/#comment-882</link>
		<dc:creator><![CDATA[David Humphreys]]></dc:creator>
		<pubDate>Thu, 07 Jan 2010 23:40:12 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1856#comment-882</guid>
		<description><![CDATA[Andrew: see http://en.support.wordpress.com/code/posting-source-code/
Jamie: I think it should be language, not lang.

A solution in Clojure.  It works fine (but slowly) for large values.  I&#039;m sure there are more elegant ways of doing it.

[sourcecode]
(ns Sum-of-Squares)
(defmacro add-squares [x y] `(+ (* ~x ~x) (* ~y ~y)))
(defn test-single-square [x y n result]
 (if (&lt; x y)
  result
  (let [sum (add-squares x y)]
   (cond
    (&lt; sum n)	#(test-single-square x (inc y) n result)
    (&gt; sum n)	#(test-single-square (dec x) y n result)
    :else	#(test-single-square (dec x) (inc y) n (conj result [x y]))))))
(defn #^{:test (fn []
 (let [check-values (fn [test-value]
  (assert (every?
   #(= test-value
    (add-squares (first %) (last %)))
    (find-squares test-value))))]
 (map check-values [1790119876545 10000798002 48612265 999 50])))}
 find-squares [n]
  (let [x (int (java.lang.Math/sqrt n)) y 0]
 (trampoline (test-single-square x y n []))))
(test #&#039;find-squares)
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Andrew: see <a href="http://en.support.wordpress.com/code/posting-source-code/" rel="nofollow">http://en.support.wordpress.com/code/posting-source-code/</a><br />
Jamie: I think it should be language, not lang.</p>
<p>A solution in Clojure.  It works fine (but slowly) for large values.  I&#8217;m sure there are more elegant ways of doing it.</p>
<pre class="brush: plain;">
(ns Sum-of-Squares)
(defmacro add-squares [x y] `(+ (* ~x ~x) (* ~y ~y)))
(defn test-single-square [x y n result]
 (if (&lt; x y)
  result
  (let [sum (add-squares x y)]
   (cond
    (&lt; sum n)	#(test-single-square x (inc y) n result)
    (&gt; sum n)	#(test-single-square (dec x) y n result)
    :else	#(test-single-square (dec x) (inc y) n (conj result [x y]))))))
(defn #^{:test (fn []
 (let [check-values (fn [test-value]
  (assert (every?
   #(= test-value
    (add-squares (first %) (last %)))
    (find-squares test-value))))]
 (map check-values [1790119876545 10000798002 48612265 999 50])))}
 find-squares [n]
  (let [x (int (java.lang.Math/sqrt n)) y 0]
 (trampoline (test-single-square x y n []))))
(test #'find-squares)
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

