Advertisement
safoyeth

factorials

Dec 6th, 2013
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.57 KB | None | 0 0
  1. #! /usr/bin/env/ python
  2. # -*-coding: utf-8-*-
  3.  
  4. import sys
  5.  
  6. def constant(function):
  7.     def wrapper(x):
  8.         if x == 1:
  9.             return [0,1]
  10.     return wrapper
  11.  
  12. def factorial(x):
  13.     if type(x) == int and x >= 0:
  14.         result = lambda x: result(x-1) * x if x > 0 else 1
  15.         return result(x)
  16.     else:
  17.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative")
  18.  
  19. @constant
  20. def antifactorial(x):
  21.     if type(x) == int and x >= 0:
  22.         for each in range(1, sys.getrecursionlimit()+1):
  23.             x = float(x)/float(each)
  24.             if x != 1.0 and x > 1.0:
  25.                 continue
  26.             elif x != 1.0 and x < 1.0:
  27.                 raise ValueError,"The given value is not a factorial"
  28.             else:
  29.                 return each
  30.                 break
  31.     else:
  32.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative")
  33.  
  34. def superfactorial(x):
  35.     if type(x) == int and x >= 0:
  36.         result = lambda x: result(x-1) * factorial(x) if x > 0 else 1
  37.         return result(x)
  38.     else:
  39.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative")
  40.  
  41. @constant
  42. def antisuperfactorial(x):
  43.     if type(x) == int and x >= 0:
  44.         for each in range(1, sys.getrecursionlimit()+1):
  45.             x = float(x)/factorial(each)
  46.             if x != 1.0 and x > 1.0:
  47.                 continue
  48.             elif x != 1.0 and x < 1.0:
  49.                 raise ValueError,"The given value is not a superfactorial"
  50.             else:
  51.                 return each
  52.                 break
  53.     else:
  54.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative")
  55.  
  56. def hyperfactorial(x):
  57.     if type(x) == int and x >= 0:
  58.         result = lambda x: result(x-1) * superfactorial(x) if x > 0 else 1
  59.         return result(x)
  60.     else:
  61.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative")
  62.  
  63. @constant
  64. def antihyperfactorial(x):
  65.     if type(x) == int and x >= 0:
  66.         for each in range(1, sys.getrecursionlimit()+1):
  67.             x = float(x)/superfactorial(each)
  68.             if x != 1.0 and x > 1.0:
  69.                 continue
  70.             elif x != 1.0 and x < 1.0:
  71.                 raise ValueError,"The given value is not a hyperfactorial"
  72.             else:
  73.                 return each
  74.                 break
  75.     else:
  76.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative")
  77.  
  78. def doublefactorial(x):
  79.     if type(x) == int and x >= 0:
  80.         result = lambda x: result(x-2) * x if x > 0 else 1
  81.         return result(x)
  82.     else:
  83.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative")
  84.  
  85. @constant
  86. def antiDoubleFactorial(x):
  87.     if type(x) == int and x >= 0:
  88.         if x % 2 == 0:
  89.             for each in [z for z in range(2, sys.getrecursionlimit()) if z % 2 == 0]:
  90.                 x = float(x)/float(each)
  91.                 if x != 1.0 and x > 1.0:
  92.                     continue
  93.                 elif x != 1.0 and x < 1.0:
  94.                     raise ValueError,"The given value is not a factorial"
  95.                 else:
  96.                     return each
  97.                     break
  98.         else:
  99.             for each in [z for z in range(1, sys.getrecursionlimit()) if z % 2 != 0]:
  100.                 x = float(x)/float(each)
  101.                 if x != 1.0 and x > 1.0:
  102.                     continue
  103.                 elif x != 1.0 and x < 1.0:
  104.                     raise ValueError,"The given value is not a factorial"
  105.                 else:
  106.                     return each
  107.                     break
  108.     else:
  109.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative"
  110.  
  111. def isPrime(x):
  112.     if type(x) == int and x >= 0:
  113.         if x == 0 or x == 1:
  114.             return False
  115.         for each in range(2,x+1):
  116.             if x % each == 0 and x != each:
  117.                 return False
  118.                 break
  119.             elif x % each == 0 and x == each:
  120.                 return True
  121.     else:
  122.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative")
  123.  
  124. def primorial(x):
  125.     if type(x) == int and x >= 0:
  126.         primes = 1
  127.         for each in range(2, x+1):
  128.             if isPrime(each) == True:
  129.                 primes *= each
  130.                 return primes
  131.     else:
  132.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative"
  133.  
  134. @constant
  135. def antiprimorial(x):
  136.     if type(x) == int and x >= 0:
  137.         for each in range(2,sys.getrecursionlimit()):
  138.             if isPrime(each) == True:
  139.                 x = float(x)/float(each)
  140.                 if x != 1.0 and x > 1.0:
  141.                     continue
  142.                 elif x != 1.0 and x < 1.0:
  143.                     raise ValueError,"The given value is not a primorial"
  144.                 else:
  145.                     values = [each]
  146.                     for var in range(1,sys.getrecursionlimit()):
  147.                         if isPrime(each+var) == False:
  148.                             values.append(each+var)
  149.                             continue
  150.                         else:
  151.                             return values
  152.                             break
  153.                     break
  154.     else:
  155.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative"
  156.  
  157. def isFactorion(x):
  158.     if type(x) == int and x >= 0:
  159.         factorion = 0
  160.         number = tuple(str(x))
  161.         for each in number:
  162.             factorion +=factorial(int(each))
  163.         if factorion == x:
  164.             return True
  165.         else:
  166.             return False
  167.     else:
  168.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative"
  169.  
  170. def isKFactorion(x,k):
  171.     if type(x) == int and x >= 0:
  172.         factorion = 0
  173.         number = tuple(str(x))
  174.         for each in number:
  175.             factorion += factorial(int(each))
  176.         if x == factorion * k:
  177.             return True
  178.         else:
  179.             return False
  180.     else:
  181.         raise TypeError, "The given value must be positive integer, not %s"%(type(x) if type(x) != int else "negative"
  182.  
  183. def sequense(function, valueFrom = 0, valueTo = 1):
  184.     return [function(x) for x in range(valueFrom, valueTo+1)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement