-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfactorial.py
148 lines (126 loc) · 3.36 KB
/
factorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#Newbie programmer
def fact1(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print fact1(6)
#First year programmer, studied Pascal
def fact2(x):
result = 1
i = 2
while i <= x:
result = result * i
i = i + 1
return result
print fact2(6)
#First year programmer, studied C
def fact3(x): #{
result = i = 1;
while (i <= x): #{
result *= i;
i += 1;
#}
return result;
#}
print(fact3(6))
#First year programmer, SICP
@tailcall
def fact4(x, acc=1):
if (x > 1): return (fact((x - 1), (acc * x)))
else: return acc
print(fact4(6))
#First year programmer, Python
def fact5(x):
res = 1
for i in xrange(2, x + 1):
res *= i
return res
print fact5(6)
#Lazy Python programmer
def fact6(x):
return x > 1 and x * fact(x - 1) or 1
print fact6(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
fact7 = lambda x: f.foldl(op.mul, 1, xrange(2, x + 1))
print fact7(6)
#Python hacker
import sys
@tailcall
def fact8(x, acc=1):
if x: return fact(x.__sub__(1), acc.__mul__(x))
return acc
sys.stdout.write(str(fact8(6)) + '\n')
#EXPERT PROGRAMMER
import c_math
fact9 = c_math.fact
print fact(6)
#ENGLISH EXPERT PROGRAMMER
import c_maths
fact10 = c_maths.fact
print fact(6)
#Unix programmer
import os
def fact11(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))