Web 2.0 programmer:<p><pre><code> import urllib
import json
def fact(n):
res = urllib.urlopen('http://math.org/factorial/?' +
urllib.urlencode({'n':n, 'format':'json'})).read()
return json.loads(res)['answer']
fact(6)
</code></pre>
Edit: for a "web scale systems guy" throw an @memcache_memoize in there :)<p>Edit2: fixed a syntax error.<p>Edit3: general cased it. thanks lolcraft!
<p><pre><code> # "close enough" programmer
import math
def fact(x):
math.sqrt(2*math.pi*n) * math.pow(n/math.e, n)
print fact(6)
# lazy-er programmer
def fact(x):
factorials = {6:720} # found on my calculator, add more as they come up
if factorials.has_key(x):
return factorials[x]
else:
raise NotImplementedError
print fact(6)</code></pre>
<a href="http://dis.4chan.org/read/prog/1180084983/" rel="nofollow">http://dis.4chan.org/read/prog/1180084983/</a><p><a href="http://www.hackerne.ws/item?id=1012877" rel="nofollow">http://www.hackerne.ws/item?id=1012877</a><p>Wow the domain can make a huge difference. :)
I'm proud to be a Lazy Python Programmer... but not lazy enough to skip "What's New in Python 2.5":<p><pre><code> def factorial(x):
return x * factorial(x-1) if x > 1 else 1</code></pre>
This is an uninspiring ripoff of the older original here:<p><a href="http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html" rel="nofollow">http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer....</a><p>And here is one that is actually well done for Haskell.<p><a href="http://www.willamette.edu/~fruehr/haskell/evolution.html" rel="nofollow">http://www.willamette.edu/~fruehr/haskell/evolution.html</a>
Java programmer:<p><pre><code> class FactorialCommand(object):
def set_num(self, num):
self.__num = num
def get_num(self):
return self.__num
def execute(self):
result = 1
i = 2
while i <= self.get_num():
result = result * i
i = i + 1
return result
cmd = FactorialCommand()
cmd.set_num(6)
print cmd.execute()</code></pre>
The "I find a way to use list comprehensions for everything" Python programmer:<p><pre><code> def fact(n):
return eval('*'.join([str(x+1) for x in xrange(n)] + ['1']))</code></pre>
Bored Python programmer:<p><pre><code> def fact(l):
return sum(map(bool,reduce(lambda I,l:''.join([l for I in I]),
['+'+'+'*l for l in xrange(l)])))
assert fact(6)==720</code></pre>
I think I'll have to contribute some J code (can anyone uglify Python this much?):<p><pre><code> (*$:@:<:)^:(1&<) 6
</code></pre>
Finds the factorial recursively. J programmers are masochists.<p>(From: <a href="http://rosettacode.org/wiki/Factorial_function#Recursive_11" rel="nofollow">http://rosettacode.org/wiki/Factorial_function#Recursive_11</a>)
"I've got a lot of memory" or "runtime is more importatant than initialisation" programmer<p><pre><code> fact = init_fact()
...
res = fact[x]</code></pre>
<a href="http://www.willamette.edu/~fruehr/haskell/evolution.html" rel="nofollow">http://www.willamette.edu/~fruehr/haskell/evolution.html</a><p>for all the closet functional programmers around here :)