Skip to content
Create a gist now

Instantly share code, notes, and snippets.

#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal
def factorial(x):
result = 1
i = 2
while i <= x:
result = result * i
i = i + 1
return result
print factorial(6)
#First year programmer, studied C
def fact(x): #{
result = i = 1;
while (i <= x): #{
result *= i;
i += 1;
#}
return result;
#}
print(fact(6))
#First year programmer, SICP
@tailcall
def fact(x, acc=1):
if (x > 1): return (fact((x - 1), (acc * x)))
else: return acc
print(fact(6))
#First year programmer, Python
def Factorial(x):
res = 1
for i in xrange(2, x + 1):
res *= i
return res
print Factorial(6)
#Lazy Python programmer
def fact(x):
return x > 1 and x * fact(x - 1) or 1
print fact(6)
#Lazier Python programmer
f = lambda x: x and x * f(x - 1) or 1
print f(6)
#Python expert programmer
import operator as op
import functional as f
fact = lambda x: f.foldl(op.mul, 1, xrange(2, x + 1))
print fact(6)
#Python hacker
import sys
@tailcall
def fact(x, acc=1):
if x: return fact(x.__sub__(1), acc.__mul__(x))
return acc
sys.stdout.write(str(fact(6)) + '\n')
#EXPERT PROGRAMMER
import c_math
fact = c_math.fact
print fact(6)
#ENGLISH EXPERT PROGRAMMER
import c_maths
fact = c_maths.fact
print fact(6)
#Web designer
def factorial(x):
#-------------------------------------------------
#--- Code snippet from The Math Vault ---
#--- Calculate factorial (C) Arthur Smith 1999 ---
#-------------------------------------------------
result = str(1)
i = 1 #Thanks Adam
while i <= x:
#result = result * i #It's faster to use *=
#result = str(result * result + i)
#result = int(result *= i) #??????
result str(int(result) * i)
#result = int(str(result) * i)
i = i + 1
return result
print factorial(6)
#Unix programmer
import os
def fact(x):
os.system('factorial ' + str(x))
fact(6)
#Windows programmer
NULL = None
def CalculateAndPrintFactorialEx(dwNumber,
hOutputDevice,
lpLparam,
lpWparam,
lpsscSecurity,
*dwReserved):
if lpsscSecurity != NULL:
return NULL #Not implemented
dwResult = dwCounter = 1
while dwCounter <= dwNumber:
dwResult *= dwCounter
dwCounter += 1
hOutputDevice.write(str(dwResult))
hOutputDevice.write('\n')
return 1
import sys
CalculateAndPrintFactorialEx(6, sys.stdout, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
#Enterprise programmer
def new(cls, *args, **kwargs):
return cls(*args, **kwargs)
class Number(object):
pass
class IntegralNumber(int, Number):
def toInt(self):
return new (int, self)
class InternalBase(object):
def __init__(self, base):
self.base = base.toInt()
def getBase(self):
return new (IntegralNumber, self.base)
class MathematicsSystem(object):
def __init__(self, ibase):
Abstract
@classmethod
def getInstance(cls, ibase):
try:
cls.__instance
except AttributeError:
cls.__instance = new (cls, ibase)
return cls.__instance
class StandardMathematicsSystem(MathematicsSystem):
def __init__(self, ibase):
if ibase.getBase() != new (IntegralNumber, 2):
raise NotImplementedError
self.base = ibase.getBase()
def calculateFactorial(self, target):
result = new (IntegralNumber, 1)
i = new (IntegralNumber, 2)
while i <= target:
result = result * i
i = i + new (IntegralNumber, 1)
return result
print StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6))
@Cam1337
Cam1337 commented Nov 3, 2011

import operator
f = lambda n: reduce(operator.mul, range(1,n+1))

@tkrajcar

Brilliantly done!

@jmg
jmg commented Dec 13, 2011

Amusing! =)

@arshaver
arshaver commented Mar 8, 2012

i = 1 #Thanks Adam
good one :)

@sourishkrout

That's aweomse. The windows API style gave me shivers.

@bertomartin

#Thanks Adam. ROFL

@davidgomes

Really good, I laughed so hard!

@forste
forste commented Mar 8, 2012

shouldn't it be '\r\n' for windows ? :D

@dnsmkl
dnsmkl commented Mar 8, 2012

+

@dimitri
dimitri commented Mar 8, 2012

I can't believe no expert programmer is memoizing the result. And using some Lisp :)

@davidgomes

Indeed, memoization or bottom-up would be the best approaches (DP).

@mpaltun
mpaltun commented Mar 8, 2012

i like it :D

@xicubed
xicubed commented Mar 8, 2012

; midlife crisis programmer (had to remove brackets around first n to post)
(defn factorial n (reduce * (range 1 (inc n))))

#probabalistic programmer:
http://threebrothers.org/brendan/blog/stirlings-approximation-formula-clojure/

@Scorpil
Scorpil commented Mar 8, 2012

Purely awesome!

@brixtonasias

I smiled.

@nremond
nremond commented Mar 8, 2012

So true, brilliant.

@2bt
2bt commented Mar 8, 2012

Really nice. Here's what I'd write:

fact = lambda x: reduce(int.__mul__, xrange(2, x + 1), 1)
@msg555
msg555 commented Mar 8, 2012

Where is the algorithmist's implementation? Divide and conquer with Fürer's algorithm is obviously the most reasonable choice.

@marcqualie

Best laugh I've had today, the web designer one is the best!

@bamdadd
bamdadd commented Mar 8, 2012

I agree, Web designer is the funniest.
Although I really like to code like the "#Python expert programmer" one :)

@jhdevos
jhdevos commented Mar 8, 2012

The first one is not very realistic; I've not seen many newbie programmers who understand recursion.
Reading the last two (windows programmer and enterprise programmer), I fell off my chair laughing.

@brianewing

The web designer one had me laughing. Great :)

@Mercurial

the php curl programmer, would curl the url above and scrap the results LOL

@captainhead

Web designer and Windows programmer made my night!

@seth-hg
seth-hg commented Mar 9, 2012

Well, "Lazier" is the best I can get at present.

@tszming
tszming commented Mar 9, 2012

you probably want some factories, abstract factories or factory factories if you want to be a successful enterprise programmer :)

@ronbeltran

It looks like I wanted to be a Newbie programmer forever!

@ddgg
ddgg commented Mar 9, 2012

#data analyst who wants to be a programmer
R
factorial(6)

@acefspds
acefspds commented Mar 9, 2012

#Laziest programmer
print 720

@ruturajv
ruturajv commented Mar 9, 2012

\m/

@kohashi
kohashi commented Mar 9, 2012
#VBA programmer
def factorial(x):
    if x == 0:
        return 1
    if x == 1:
        return x
    if x == 2:
        return x * (x-1)
    if x == 3:
        return x * (x-1) * (x-2)
    if x == 4:
        return x * (x-1) * (x-2) * (x-3)
    if x == 5:
        return x * (x-1) * (x-2) * (x-3) * (x-4)
    if x == 6:
        return x * (x-1) * (x-2) * (x-3) * (x-4) * (x-5)
print factorial(6)

I actually saw this code at post office.

@rspeed
rspeed commented Mar 9, 2012
from math import factorial
print factorial(6)
@obeleh
obeleh commented Mar 9, 2012
#Optimized for performance and size
def factorial(x):
    if x == 0:
        return 1
    if x == 1:
        return 1
    if x == 2:
        return 2
    if x == 3:
        return 6
    if x == 4:
        return 24
    if x == 5:
        return 120
    if x == 6:
        return 720
print factorial(6)
@mrahma01
mrahma01 commented Mar 9, 2012

I love the enterprise programmer :P
have a life dude !

@radames
radames commented Mar 9, 2012

uaau very good!!

@DVDPT
DVDPT commented Mar 9, 2012

Haha windows programmer FTW

@troolee
troolee commented Mar 9, 2012

@obeleh

import c_math
def factorial(x):
    return {
        0: 1,
        1: 1,
        2: 2,
        3: 6,
        4: 24,
        5: 120,
        6: 720
    }.get(x) or c_math.fact(x)
@yaph
yaph commented Mar 9, 2012

Very intriguing, especially the very realistic portrayal of enterprise programmers, though they probably manage to write even more complex solutions in their preferred language Java.

@bobardkor

Scumbag programmer:

def factorial():
  return 720
print factorial(6) # it works !
@rjw57
rjw57 commented Mar 9, 2012

Engineer:

import math
print math.exp(x * math.log(x) - x)
@ali65
ali65 commented Mar 9, 2012

Scientist:

import scipy.misc
def factorial(n): scipy.misc.factorial(n,True)
print factorial(6)
@hseagle2015

Awesome, made my day! :-)

@mcandre
mcandre commented Mar 9, 2012

Yes, yes, yes!

@gandaro
gandaro commented Mar 9, 2012

That’s my style:

#!/usr/bin/env python3
# compatible from Python 2.6 to Python 3.2
from operator import mul
from functools import reduce

# range can only handle ranges up to sys.maxint
def iterrange(a, b):
    while a < b:
        yield a
        a += 1

def factorial(x):
    assert x > 0, 'x has to be greater than or equal to 0'
    return reduce(mul, iterrange(2, x+1), 1)


if __name__ == '__main__':
    print(factorial(6))
@luke14free

Startup guy obsessed with caching everything:

from functools import wraps
def memo(func): 
    cache = {}
    @wraps(func)
    def wrap(*args):
       if args not in cache: cache[args] = func(*args)
       return cache[args] 
    return wrap

@memo
def fact(x):
    return (1<x<1000 and x*fact(x-1)) or 1 # possibly raise VCsDontPayMeEnoughToExecuteThisQueryError('true story.')
@chasingtheflow

haha love it. curious what you mean by "english expert programmer"

@chasingtheflow
@troolee
troolee commented Mar 11, 2012

@gandaro
No, it's not.

 >>> def aa(x):
...     print 'aa'
...     return 20
... 
>>> print {1:1, 10:10}.get(10, aa(10))
aa
10
>>> print {1:1, 10:10}.get(10) or aa(10)
10
>>>

a or b or c or d - in this case arguments will be calculated lazy. In case of {a:a}.get(x, func(x)) func(x) will be calculeted before pass it to get

@gandaro
gandaro commented Mar 11, 2012

@troolee That has nothing to do with the snippet above, but you are right about the lazy evaluation. That would be inefficient.

@ghdawn
ghdawn commented Mar 13, 2012

amazing! Windows programmer is great.....

@yaph
yaph commented Mar 30, 2012

@radiosilence OMG in my run with 200 rounds the Windows Programmer is the fastest

@radiosilence
@nremond
nremond commented Mar 30, 2012

@radiosilence @yaph on my windows machine, the "Entreprise Programmer" is the faster ... haha

@yaph
yaph commented Mar 30, 2012

@nire thats even worse :D

@radiosilence
@gandaro
gandaro commented Mar 30, 2012

@radiosilence: Why don’t you use timeit.Timer?

@radiosilence
@yaph
yaph commented Mar 30, 2012

@gandaro well, then it's no surprise that Windows programmer and Enterprise programmer did well ;)

@radiosilence

Gandaro - Oh FUCK.

Could you do a pull request?

@radiosilence

Here are some results now it's fixed:

Recursion limit: 4000
========================================================
Starting algorithm Newbie for 2000!
========================================================
Did it in 9359.77697372 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm First Year Pascal for 2000!
========================================================
Did it in 5224.70474243 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm First Year C for 2000!
========================================================
Did it in 5262.83979416 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm First Year SICP for 2000!
========================================================
Did it in 12595.5700874 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm First Year Python for 2000!
========================================================
Did it in 4722.47600555 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Lazy Python for 2000!
========================================================
Did it in 9027.01616287 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Lazier Python for 2000!
========================================================
Did it in 8971.9414711 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Python Expert for 2000!
========================================================
Did it in 13691.5564537 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Python Hacker for 2000!
========================================================
Did it in 14962.4228477 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Expert Programmer for 2000!
========================================================
Did it in 4450.71458817 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================

========================================================
Starting algorithm Web Designer for 2000!
========================================================
Did it in 2142111.67097 microsecs averaged over 20 rounds.
========================================================
Answer was correct? False
========================================================

========================================================
Starting algorithm Unix Programmer for 2000!
========================================================
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
sh: factorial: command not found
Did it in 14699.4709969 microsecs averaged over 20 rounds.
========================================================
sh: factorial: command not found
Answer was correct? False
========================================================

========================================================
Starting algorithm Windows Programmer for 2000!
========================================================
Did it in 5209.56516266 microsecs averaged over 20 rounds.
========================================================
Answer was correct? False
========================================================

========================================================
Starting algorithm Enterprise Programmer for 2000!
========================================================
Did it in 13747.048378 microsecs averaged over 20 rounds.
========================================================
Answer was correct? True
========================================================
@msg555
msg555 commented Apr 3, 2012
@gandaro
@mcandre
@caruccio

Nice!
Also, a google engineer would use a map-reduce approach with thousands of nodes.

@muxcmux
muxcmux commented May 15, 2012

I love how the C programmer has commented on the curly braces :D

@nickpoorman

This is too funny.

@pythonjunkie

Very nicely written..!!! Good job..!

@pydsigner

Nice, LOL

@WarFox
WarFox commented Sep 24, 2012

fascinating...!

@Ulu2005
Ulu2005 commented Nov 29, 2012

LOL!

@Zenithar
Zenithar commented Dec 4, 2012

Lol ! And so desperately true ...

@4gn3s
4gn3s commented Dec 20, 2012

awesome! love especially windows programmer :)

@gnaggnoyil

ROFL :) the windows programmmer makes me laugh

@whardier
whardier commented May 3, 2013

shakes head

@SubhrajitPrusty

ROFLMAO when I read the windows programmer...:D

@ghost
Unknown commented May 25, 2013

HAHAHAHAHAHAHA brilliant!!!!

@realduke2000

5 years Newbie programmer....

@Strech
Strech commented Jul 4, 2013

awesome

@minaeid90

The windows one is awful

@abhinavongraph

Awesome Man

@evecalm
evecalm commented Nov 13, 2013

#Enterprise programmer
That's java's style.

@openinx
openinx commented Nov 13, 2013

非常讽刺啊。。。。

@hustlijian

penta kill ! god like!

@iljyya
iljyya commented Nov 14, 2013

哈哈

@Hacker-YHJ

LOL, the C-Style is really funny.

@neerajsharma7

brilliant :)

@nickschurch

Can I add what is clearly the best and most elegant solution to the mix:

#Python data scientist
import scipy.misc as sc
sc.factorial(6)
@oskar-j
oskar-j commented Jun 11, 2015

Very educational and brilliant

@chesterwang

haha

@xmnlab
xmnlab commented Jun 13, 2015

great!

@iamwucheng

'awesome'*sys.maxint, LOL

@pavdmyt
pavdmyt commented Jul 16, 2015
# Python programmer with math background
import math as m
def fact(x):
    return m.exp(sum([m.log(k) for k in range(1, x+1)]))
@faizalpribadi

Hahahaha 👍 Nice Man 👯

@felipsmartins

Windows programer LOL

@jemyzhang

@bobardkor great work! :>)

@cwyark
cwyark commented May 7, 2016

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.