Obfuscated factorial

S

Steven D'Aprano

Just for fun:


class Numberator:
def __init__(self, number):
self.__number = number
def evaluate(self):
return self.__number

class Multiplier:
def __init__(self, multiplier, multiplicand):
self.multiplier = multiplier
self.multiplicand = multiplicand
def evaluate(self):
return self.multiplier * self.multiplicand.evaluate()

class FactorialBuilder:
def __init__(self, number):
if not isinstance(number, int):
raise TypeError
if number < 0:
raise ValueError
self.__number = number
def build(self):
multiplicand = Numberator(1)
for n in range(2, self.__number + 1):
multiplicand = Multiplier(n, multiplicand)
return Factorialiser(multiplicand)

class Factorialiser:
def __init__(self, evaluatorix):
self.__evaluatorix = evaluatorix
def calculate(self):
return self.__evaluatorix.evaluate()

for i in range(16):
print(i, FactorialBuilder(i).build().calculate())



And the output is:

0 1
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
11 39916800
12 479001600
13 6227020800
14 87178291200
15 1307674368000



It goes without saying that this isn't Pythonic :)
 
C

Chris Angelico

you miss a FactoryFactory and a couple of *Manager.

Oh, and it should be xml-config-driven.

Or a metaclass and a few decorators. They're inherently Pythonic, right?

ChrisA
 
G

Gene Heskett

Just for fun:


class Numberator:
def __init__(self, number):
self.__number = number
def evaluate(self):
return self.__number

class Multiplier:
def __init__(self, multiplier, multiplicand):
self.multiplier = multiplier
self.multiplicand = multiplicand
def evaluate(self):
return self.multiplier * self.multiplicand.evaluate()

class FactorialBuilder:
def __init__(self, number):
if not isinstance(number, int):
raise TypeError
if number < 0:
raise ValueError
self.__number = number
def build(self):
multiplicand = Numberator(1)
for n in range(2, self.__number + 1):
multiplicand = Multiplier(n, multiplicand)
return Factorialiser(multiplicand)

class Factorialiser:
def __init__(self, evaluatorix):
self.__evaluatorix = evaluatorix
def calculate(self):
return self.__evaluatorix.evaluate()

for i in range(16):
print(i, FactorialBuilder(i).build().calculate())



And the output is:

0 1
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
11 39916800
12 479001600
13 6227020800
14 87178291200
15 1307674368000
Nice, but will it go to 70? :)

It goes without saying that this isn't Pythonic :)


Cheers, Gene
--
"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)

"Out of register space (ugh)"
-- vi
A pen in the hand of this president is far more
dangerous than 200 million guns in the hands of
law-abiding citizens.
 
J

Joshua Landau

Just for fun: ....
for i in range(16):
print(i, FactorialBuilder(i).build().calculate())

Python already supports the factorial operator, -ⵘ. You just have to import it.

# Import statement
ⵘ = type("",(),{"__rsub__":lambda s,n:(lambda f,n:f(f,n))(lambda
f,z:round((2.5066282746310002*(z+0j+7.5)**(z+0.5)*2.718281828459045**(-z-7.5)*(0.99999999999980993+676.5203681218851/(z+1)-1259.1392167224028/(z+2)+771.32342877765313/(z+3)-176.61502916214059/(z+4)+12.507343278686905/(z+5)-0.13857109526572012/(z+6)+9.9843695780195716e-6/(z+7)+1.5056327351493116e-7/(z+8))).real)if
z>=0.5 else 3.141592653589793/(__import__("math").sin(3.141592653589793*z)*f(f,1-z)),n)})()

assert 2-ⵘ == 2

assert 3-ⵘ == 6

assert 4-ⵘ == 24
 
T

Tim Chase

Or a metaclass and a few decorators. They're inherently Pythonic,
right?

You are deploying it to The Cloudâ„¢ right? And where is the
horizontal scalability? Why doesn't it use NoSQL?

Bah, just goes to show that Python is no good for Enterpriseâ„¢
applications ;-)

-tkc
 
V

Vito De Tullio

Joshua said:
Python already supports the factorial operator, -ⵘ.

why use ⵘ (TIFINAGH LETTER AYER YAGH) when you can have the BANG? 방 (HANGUL
SYLLABLE BANG)

You just have to
import it.

# Import statement
ⵘ = type("",(),{"__rsub__":lambda s,n:(lambda f,n:f(f,n))(lambda
f,z:round((2.5066282746310002*(z+0j+7.5)**(z+0.5)*2.718281828459045**(- z-7.5)*(0.99999999999980993+676.5203681218851/(z+1)-1259.1392167224028/(z+2)+771.32342877765313/(z+3)-176.61502916214059/(z+4)+12.507343278686905/(z+5)-0.13857109526572012/(z+6)+9.9843695780195716e-6/(z+7)+1.5056327351493116e-7/(z+8))).real)if
z>=0.5 else
3.141592653589793/(__import__("math").sin(3.141592653589793*z)*f(f,1-
z)),n)})()

I'm really, really impressed!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top