• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# extendedmath

safoyeth Dec 11th, 2013 (edited) 474 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #! /usr/bin/env/ python
2. # -*-coding: utf-8-*-
3.
4. import sys
5. from functools import wraps
6.
7. def isInteger(function):
8.     @wraps(function)
9.     def wrapper(*args):
10.         if all(type(each) == int and each >= 0 for each in args):
11.             return function(*args)
12.         else:
13.             raise TypeError, "The given value must be positive integer, got %s"%(str(args) + " instead" if any(type(each) != int for each in args) else "negative integer instead")
14.     return wrapper
15.
16. def manager(argument = None, entity = None):
17.     def decorator(function):
18.         @wraps(function)
19.         def wrapper(x):
20.             if x == 1:
21.                 return [0, 1]
22.             else:
23.                 for each in ([z for z in range(2, sys.getrecursionlimit()) if z % 2 == 0] if argument == "double" and x % 2 == 0 else
24.                              [z for z in range(2, sys.getrecursionlimit()) if z % 2 != 0] if argument == "double" and x % 2 != 0 else
25.                               sequense(isPrime, 2, sys.getrecursionlimit()) if argument == "prime" else
26.                               range(2, sys.getrecursionlimit()+1)):
27.                     x = float(x)/(factorial(each) if argument == "super"
28.                                  else superfactorial(each) if argument == "hyper"
29.                                  else float(each))
30.                     if x != 1.0 and x > 1.0:
31.                         continue
32.                     elif x != 1.0 and x < 1.0:
33.                         if entity:
34.                             return False
35.                         else:
36.                             raise ValueError,"The given value is not correct!"
37.                     else:
38.                         if argument == "prime":
39.                             values = [each]
40.                             for var in range(1, sys.getrecursionlimit()):
41.                                 if not isPrime(each+var):
42.                                     values.append(each+var)
43.                                     continue
44.                                 else:
45.                                     return values
46.                                     break
47.                             break
48.                         else:
49.                             return True if entity else each
50.                             break
51.                 return function(x)
52.         return wrapper
53.     return decorator
54.
55. def factorions(argument = None):
56.     def decorator(function):
57.         @wraps(function)
58.         def wrapper(x, k):
59.             if k == 0:
60.                 raise ValueError, "0-factorions cannot be defined!"
61.             else:
62.                 factorion = 0
63.                 number = tuple(str(x))
64.                 for each in number:
65.                     factorion += (factorial(int(each)) if not argument else
66.                                   superfactorial(int(each)) if argument == "super" else
67.                                   hyperfactorial(int(each)) if argument == "hyper" else
68.                                   doublefactorial(int(each)) if argument == "double" else
69.                                   primorial(int(each)))
70.                     if x == factorion * k:
71.                         return True
72.                     else:
73.                         return False
74.         return wrapper
75.     return decorator
76.
77. @isInteger
78. def factorial(x):
79.     result = lambda x: result(x-1) * x if x > 0 else 1
80.     return result(x)
81.
82. @isInteger
83. def superfactorial(x):
84.     result = lambda x: result(x-1) * factorial(x) if x > 0 else 1
85.     return result(x)
86.
87. @isInteger
88. def hyperfactorial(x):
89.     result = lambda x: result(x-1)* superfactorial(x) if x > 0 else 1
90.     return result(x)
91.
92. @isInteger
93. def doublefactorial(x):
94.     result = lambda x: result(x-2) * x if x > 0 else 1
95.     return result(x)
96.
97. @isInteger
98. def primorial(x):
99.     primes = 1
100.     for each in range(1, x+1):
101.         if isPrime(each):
102.             primes *= each
103.     return primes
104.
105. @isInteger
106. @manager()
107. def antifactorial(x):
108.     return x
109.
110. @isInteger
111. @manager("super")
112. def antisuperfactorial(x):
113.     return x
114.
115. @isInteger
116. @manager("hyper")
117. def antihyperfactorial(x):
118.     return x
119.
120. @isInteger
121. @manager("double")
122. def antidoublefactorial(x):
123.     return x
124.
125. @isInteger
126. @manager("prime")
127. def antiprimorial(x):
128.     return(x)
129.
130. @isInteger
131. @manager(None, True)
132. def isFactorial(x):
133.     return x
134.
135. @isInteger
136. @manager("super", True)
137. def isSuperfactorial(x):
138.     return x
139.
140. @isInteger
141. @manager("hyper", True)
142. def isHyperfactorial(x):
143.     return x
144.
145. @isInteger
146. @manager("double", True)
147. def isDoublefactorial(x):
148.     return x
149.
150. @isInteger
151. @manager("prime", True)
152. def isPrimorial(x):
153.     return(x)
154.
155. @isInteger
156. def isPrime(x):
157.     if x == 0 or x == 1:
158.         return False
159.     else:
160.         for each in range(2,x+1):
161.             if x % each == 0 and x != each:
162.                 return False
163.                 break
164.             elif x % each == 0 and x == each:
165.                 return True
166.
167. @isInteger
168. def isFactorion(x):
169.     return isKFactorion(x, 1)
170.
171. @isInteger
172. def isSuperfactorion(x):
173.     return isKSuperfactorion(x, 1)
174.
175. @isInteger
176. def isHyperfactorion(x):
177.     return isKHyperfactorion(x, 1)
178.
179. @isInteger
180. def isDoublefactorion(x):
181.     return isKDoublefactorion(x, 1)
182.
183. @isInteger
184. def isPrimorion(x):
185.     return isKPrimorion(x, 1)
186.
187. @isInteger
188. @factorions()
189. def isKFactorion(x,k):
190.     return x
191.
192. @isInteger
193. @factorions("super")
194. def isKSuperfactorion(x,k):
195.     return x
196.
197. @isInteger
198. @factorions("hyper")
199. def isKHyperfactorion(x,k):
200.     return x
201.
202. @isInteger
203. @factorions("double")
204. def isKDoublefactorion(x,k):
205.     return x
206.
207. @isInteger
208. @factorions("prime")
209. def isKPrimorion(x,k):
210.     return x
211.
212. def sequence(function, valueFrom = 0, valueTo = 1, reverse = False):
213.     result = []
214.     seq = [function(x) for x in range(valueFrom, valueTo+1)]
215.     if False in seq:
216.         for index, value in enumerate(seq):
217.             if value:
218.                 result.append(index+valueFrom)
219.     else:
220.         result = seq
221.     return result if not reverse else result[::-1]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top