<?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/"
		>
<channel>
	<title>Comments on: Pythagoras and lambdas&#8230;..</title>
	<atom:link href="http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/feed/" rel="self" type="application/rss+xml" />
	<link>http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/</link>
	<description>In Pursuit Of Truth and Beauty</description>
	<lastBuildDate>Tue, 07 Feb 2012 15:11:05 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: jmoney</title>
		<link>http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/comment-page-1/#comment-707</link>
		<dc:creator>jmoney</dc:creator>
		<pubDate>Mon, 25 Aug 2008 13:32:46 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=214#comment-707</guid>
		<description>I recommend cutting your looping down.
p hyp  1000)

Just ideas... brute force helps clarify the problems, allowing much more elegant solutions.</description>
		<content:encoded><![CDATA[<p>I recommend cutting your looping down.<br />
p hyp  1000)</p>
<p>Just ideas&#8230; brute force helps clarify the problems, allowing much more elegant solutions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rgz</title>
		<link>http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/comment-page-1/#comment-705</link>
		<dc:creator>rgz</dc:creator>
		<pubDate>Mon, 25 Aug 2008 04:03:49 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=214#comment-705</guid>
		<description>The real speedup would be using a generator. And while we are at it let&#039;s use argument unpacking and the built-in iteritems.

dict((v,k) for k, v in d.iteritems())</description>
		<content:encoded><![CDATA[<p>The real speedup would be using a generator. And while we are at it let&#8217;s use argument unpacking and the built-in iteritems.</p>
<p>dict((v,k) for k, v in d.iteritems())</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shriphani</title>
		<link>http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/comment-page-1/#comment-684</link>
		<dc:creator>Shriphani</dc:creator>
		<pubDate>Sun, 24 Aug 2008 01:47:17 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=214#comment-684</guid>
		<description>@Jim Meier: Thanks for your solution as well.</description>
		<content:encoded><![CDATA[<p>@Jim Meier: Thanks for your solution as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shriphani</title>
		<link>http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/comment-page-1/#comment-683</link>
		<dc:creator>Shriphani</dc:creator>
		<pubDate>Sun, 24 Aug 2008 01:43:54 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=214#comment-683</guid>
		<description>@seth: Thanks for pointing out the improvement in efficiency by using a list comprehension instead of the lambda+map thingy. And yeah, I should&#039;ve seen that a+b+c&lt; 1000 is easier to evaluate than the other condition.</description>
		<content:encoded><![CDATA[<p>@seth: Thanks for pointing out the improvement in efficiency by using a list comprehension instead of the lambda+map thingy. And yeah, I should&#8217;ve seen that a+b+c< 1000 is easier to evaluate than the other condition.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Meier</title>
		<link>http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/comment-page-1/#comment-682</link>
		<dc:creator>Jim Meier</dc:creator>
		<pubDate>Sun, 24 Aug 2008 01:34:28 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=214#comment-682</guid>
		<description>Of course, a better solution is to reduce the problem from an O(n^3) to O(n^2) with minimal constant factors:

&lt;pre&gt;
from math import sqrt, ceil

def solve(p):
    max_unique_hyp = int(ceil(p/2))
    for hyp in xrange(1, max_unique_hyp):
        hyp2 = hyp ** 2
        for x in xrange(1, hyp):
            y = sqrt(hyp2 - x**2)
            if y == int(y) and x+y+hyp  most_solutions[1]:
            most_solutions = (p, len(s))
    return solutions, most_solutions

print most_solutions(1000)
&lt;/pre&gt;
I&#039;m not sure how that&#039;ll show up formatted as a comment, sorry.

The results:
&lt;pre&gt;
$ time python peri.py
(840.0, 8)

real	0m0.198s
user	0m0.180s
sys	0m0.013s&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Of course, a better solution is to reduce the problem from an O(n^3) to O(n^2) with minimal constant factors:</p>
<pre>
from math import sqrt, ceil

def solve(p):
    max_unique_hyp = int(ceil(p/2))
    for hyp in xrange(1, max_unique_hyp):
        hyp2 = hyp ** 2
        for x in xrange(1, hyp):
            y = sqrt(hyp2 - x**2)
            if y == int(y) and x+y+hyp  most_solutions[1]:
            most_solutions = (p, len(s))
    return solutions, most_solutions

print most_solutions(1000)
</pre>
<p>I&#8217;m not sure how that&#8217;ll show up formatted as a comment, sorry.</p>
<p>The results:</p>
<pre>
$ time python peri.py
(840.0, 8)

real	0m0.198s
user	0m0.180s
sys	0m0.013s</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Seth</title>
		<link>http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/comment-page-1/#comment-676</link>
		<dc:creator>Seth</dc:creator>
		<pubDate>Sun, 24 Aug 2008 00:20:50 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=214#comment-676</guid>
		<description>Also, a little poking showed that reversing the conditions in checkCond saves a substantial amount of time.

a + b + c &lt; 1000 and c ** 2 == a ** 2 + b ** 2

It&#039;s often a good idea to be very deliberate about arranging conditions in an AND statement. Sometimes you want the most likely to be violated, other times you want the one that&#039;s cheapest to evaluate. Python (and many other languages) won&#039;t test later conditions in an AND after one fails.

I&#039;ll stop poking at your code now. Hope my tips are useful to you.</description>
		<content:encoded><![CDATA[<p>Also, a little poking showed that reversing the conditions in checkCond saves a substantial amount of time.</p>
<p>a + b + c &lt; 1000 and c ** 2 == a ** 2 + b ** 2</p>
<p>It&#8217;s often a good idea to be very deliberate about arranging conditions in an AND statement. Sometimes you want the most likely to be violated, other times you want the one that&#8217;s cheapest to evaluate. Python (and many other languages) won&#8217;t test later conditions in an AND after one fails.</p>
<p>I&#8217;ll stop poking at your code now. Hope my tips are useful to you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Seth</title>
		<link>http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/comment-page-1/#comment-675</link>
		<dc:creator>Seth</dc:creator>
		<pubDate>Sat, 23 Aug 2008 23:51:25 +0000</pubDate>
		<guid isPermaLink="false">http://shriphani.com/blog/?p=214#comment-675</guid>
		<description>I&#039;d be remiss as a Python nerd if I didn&#039;t point out that you could speed things up and avoid the lambda/map if you used a list comprehension.

dict([(item[1], item[0]) for item in dict_name.items()])

Keep up the good work!</description>
		<content:encoded><![CDATA[<p>I&#8217;d be remiss as a Python nerd if I didn&#8217;t point out that you could speed things up and avoid the lambda/map if you used a list comprehension.</p>
<p>dict([(item[1], item[0]) for item in dict_name.items()])</p>
<p>Keep up the good work!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

