【发布时间】:2012-04-06 12:19:02
【问题描述】:
我一直在学习函数式编程,我想到了组装数学运算符。
counting -> addition -> multiplication -> power -> ... 自然而然地出现了简单且最天真的代码来表达这一点,并且它有效!问题是我真的不知道为什么它工作得这么好并且输出这么大。
问题是: 这个函数的复杂度是多少?
代码在python中:
def operator(d):
if d<=1:
return lambda x,y:x+y
else:
return lambda x,y:reduce(operator(d-1),(x for i in xrange(y)))
#test
f1 = operator(1) #f1 is adition
print("f1",f1(50,52)) #50+52
f2 = operator(2) #f2 is multiplication
print("f2",f2(2,20)) #2*20
f3 = operator(3) #f3 is power, just look how long output can be
print("f3",f3(4,100)) #4**100
f4 = operator(4) #f4 is superpower, this one does not work that well
print("f4",f4(2,6)) #((((2**2)**2)**2)**2)**2
f5 = operator(5) #f5 do not ask about this one,
print("f5",f5(2,4)) #
输出(即时):
('f1', 102)
('f2', 40)
('f3', 1606938044258990275541962092341162602522202993782792835301376L)
('f4', 4294967296L)
('f5', 32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656L)
【问题讨论】:
-
您的代码仅在递归被展平时才执行加法操作,所以我猜 Python bignums 在加法方面真的高效。
-
@Frederic:不过,添加的数量呈指数增长。尽管如此,这里提供的数字的数量并不是真正惊人的。我没有看到问题,更不用说具体的问题了。
-
您应该尝试对代码添加的数量进行基准测试 - 只需稍微更改
operator(1)以在调用变量时递增变量。顺便说一句,我感觉您可能对Ackerman function 感兴趣:)。 -
@missingno Ackerman 实际上是以其他方式定义的。例如 operator(4)(2,3) 是 (22)**2 其中 ACK(4,0) 是 2**(22)-3
标签: python recursion functional-programming closures complexity-theory