<?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: One-line factorial function in Python</title>
	<atom:link href="http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/</link>
	<description></description>
	<lastBuildDate>Tue, 27 Jan 2009 20:02:03 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Tristam MacDonald</title>
		<link>http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5552</link>
		<dc:creator>Tristam MacDonald</dc:creator>
		<pubDate>Tue, 21 Oct 2008 21:21:37 +0000</pubDate>
		<guid isPermaLink="false">http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5552</guid>
		<description>Unfortunately, neither Peter or David actually defined a factorial function - in particular, neither handles factorial(1) or factorial(0).</description>
		<content:encoded><![CDATA[<p>Unfortunately, neither Peter or David actually defined a factorial function &#8211; in particular, neither handles factorial(1) or factorial(0).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Borsheim</title>
		<link>http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5502</link>
		<dc:creator>David Borsheim</dc:creator>
		<pubDate>Mon, 18 Aug 2008 21:40:03 +0000</pubDate>
		<guid isPermaLink="false">http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5502</guid>
		<description>Now that I go back and read the comments, I see that others were there before me.  Oh well...  No real harm done...

Best regards, 

Borsheim</description>
		<content:encoded><![CDATA[<p>Now that I go back and read the comments, I see that others were there before me.  Oh well&#8230;  No real harm done&#8230;</p>
<p>Best regards, </p>
<p>Borsheim</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Borsheim</title>
		<link>http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5501</link>
		<dc:creator>David Borsheim</dc:creator>
		<pubDate>Mon, 18 Aug 2008 21:24:33 +0000</pubDate>
		<guid isPermaLink="false">http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5501</guid>
		<description>I realize that this is a very old post, but I couldn&#039;t resist throwing my 2 cents in...

Here is an actual 1-line factorial:

seed = 
reduce(lambda x, y: x * y, xrange(1, seed+1))

or as a literal
factorial_10 = reduce(lambda x, y: x * y, xrange(1, 11)) # 10 + 1

I use xrange() because range() chokes on very large numbers.  Even so, if you send if off to return the factorial of 1,000,000, you will wait a while.

By way of example, the factorial of 1,000 == &quot;402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L&quot;

Best regards, 

Borsheim</description>
		<content:encoded><![CDATA[<p>I realize that this is a very old post, but I couldn&#8217;t resist throwing my 2 cents in&#8230;</p>
<p>Here is an actual 1-line factorial:</p>
<p>seed =<br />
reduce(lambda x, y: x * y, xrange(1, seed+1))</p>
<p>or as a literal<br />
factorial_10 = reduce(lambda x, y: x * y, xrange(1, 11)) # 10 + 1</p>
<p>I use xrange() because range() chokes on very large numbers.  Even so, if you send if off to return the factorial of 1,000,000, you will wait a while.</p>
<p>By way of example, the factorial of 1,000 == &#8220;402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L&#8221;</p>
<p>Best regards, </p>
<p>Borsheim</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jameson</title>
		<link>http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5474</link>
		<dc:creator>Jameson</dc:creator>
		<pubDate>Thu, 26 Jun 2008 07:50:03 +0000</pubDate>
		<guid isPermaLink="false">http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5474</guid>
		<description>python needs a carriage returns followed by an indentation after defining a function... do it like this:

&lt;cite&gt;
&gt;&gt;&gt; def fact(x):
...     return (1 if x==0 else x * fact(x-1))
... 
&gt;&gt;&gt; fact(1)
1
&gt;&gt;&gt; fact(10)
3628800
&lt;/cite&gt;</description>
		<content:encoded><![CDATA[<p>python needs a carriage returns followed by an indentation after defining a function&#8230; do it like this:</p>
<p><cite><br />
&gt;&gt;&gt; def fact(x):<br />
&#8230;     return (1 if x==0 else x * fact(x-1))<br />
&#8230;<br />
&gt;&gt;&gt; fact(1)<br />
1<br />
&gt;&gt;&gt; fact(10)<br />
3628800<br />
</cite></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Hartley</title>
		<link>http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5473</link>
		<dc:creator>Michael Hartley</dc:creator>
		<pubDate>Wed, 25 Jun 2008 07:23:29 +0000</pubDate>
		<guid isPermaLink="false">http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5473</guid>
		<description>err.. pardon my ignorance of large non-venomous snakes, but how do I actually use this? Here&#039;s what happened when I tried :

&lt;code&gt;[mikeh@pud21 ~]$ python
Python 2.4.3 (#1, Mar 14 2007, 19:01:42) 
[GCC 4.1.1 20070105 (Red Hat 4.1.1-52)] on linux2
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; def fact(x): return (1 if x==0 else x * fact(x-1))
  File &quot;&quot;, line 1
    def fact(x): return (1 if x==0 else x * fact(x-1))
                            ^
SyntaxError: invalid syntax
&gt;&gt;&gt; &lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>err.. pardon my ignorance of large non-venomous snakes, but how do I actually use this? Here&#8217;s what happened when I tried :</p>
<p><code>[mikeh@pud21 ~]$ python<br />
Python 2.4.3 (#1, Mar 14 2007, 19:01:42)<br />
[GCC 4.1.1 20070105 (Red Hat 4.1.1-52)] on linux2<br />
Type "help", "copyright", "credits" or "license" for more information.<br />
&gt;&gt;&gt; def fact(x): return (1 if x==0 else x * fact(x-1))<br />
  File "", line 1<br />
    def fact(x): return (1 if x==0 else x * fact(x-1))<br />
                            ^<br />
SyntaxError: invalid syntax<br />
&gt;&gt;&gt; </code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aditya</title>
		<link>http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5467</link>
		<dc:creator>Aditya</dc:creator>
		<pubDate>Tue, 03 Jun 2008 06:44:06 +0000</pubDate>
		<guid isPermaLink="false">http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-5467</guid>
		<description>Thanks for the information Peter, but can&#039;t you just give the command &#039;time&#039; while calculating the factorial? I mean, give the command,

time python programName.py

it will output the time. Built-in time function for all *nix systems.</description>
		<content:encoded><![CDATA[<p>Thanks for the information Peter, but can&#8217;t you just give the command &#8216;time&#8217; while calculating the factorial? I mean, give the command,</p>
<p>time python programName.py</p>
<p>it will output the time. Built-in time function for all *nix systems.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Hosey</title>
		<link>http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-16</link>
		<dc:creator>Peter Hosey</dc:creator>
		<pubDate>Mon, 25 Dec 2006 19:24:58 +0000</pubDate>
		<guid isPermaLink="false">http://importantshock.wordpress.com/2006/11/03/one-line-factorial-function-in-python/#comment-16</guid>
		<description>Here&#039;s an alternate solution, unless you count the import operator statement…

&lt;code&gt;def fact(x): return reduce(operator.mul, xrange(2, x+1))&lt;/code&gt;

If you want to be really strict about it, you can do this:

&lt;code&gt;def fact(x): return reduce(lambda x, y: x*y, xrange(2, x+1))&lt;/code&gt;

but it probably won&#039;t be as fast, since you&#039;re now using a Python function in place of a built-in.

As for exactly how these two functions stack up against yours, here&#039;s what I get on my Mac Pro:

&lt;blockquote&gt;&gt;&gt;&gt; time_ptfact(500, 5000)
0.00046299901008605955
&gt;&gt;&gt; time_reducefact(500, 5000)
0.00028626961708068849
&gt;&gt;&gt; time_reducefact_lambda(500, 5000)
0.0003613057613372803&lt;/blockquote&gt;

Shows once again the value of doing as much as possible in C code (reduce, operator.mul, and xrange all being built-ins).

The timing functions are defined as:

&lt;blockquote&gt;def time_FUNC(x, count=500):
	import time
	start = time.time()
	for i in xrange(count):
		FUNC(x) and None
	end = time.time()
	return (end - start) / count&lt;/blockquote&gt;

for all three values of FUNC.</description>
		<content:encoded><![CDATA[<p>Here&#8217;s an alternate solution, unless you count the import operator statement…</p>
<p><code>def fact(x): return reduce(operator.mul, xrange(2, x+1))</code></p>
<p>If you want to be really strict about it, you can do this:</p>
<p><code>def fact(x): return reduce(lambda x, y: x*y, xrange(2, x+1))</code></p>
<p>but it probably won&#8217;t be as fast, since you&#8217;re now using a Python function in place of a built-in.</p>
<p>As for exactly how these two functions stack up against yours, here&#8217;s what I get on my Mac Pro:</p>
<blockquote><p>&gt;&gt;&gt; time_ptfact(500, 5000)<br />
0.00046299901008605955<br />
&gt;&gt;&gt; time_reducefact(500, 5000)<br />
0.00028626961708068849<br />
&gt;&gt;&gt; time_reducefact_lambda(500, 5000)<br />
0.0003613057613372803</p></blockquote>
<p>Shows once again the value of doing as much as possible in C code (reduce, operator.mul, and xrange all being built-ins).</p>
<p>The timing functions are defined as:</p>
<blockquote><p>def time_FUNC(x, count=500):<br />
	import time<br />
	start = time.time()<br />
	for i in xrange(count):<br />
		FUNC(x) and None<br />
	end = time.time()<br />
	return (end &#8211; start) / count</p></blockquote>
<p>for all three values of FUNC.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
