Skip to content

Instantly share code, notes, and snippets.

@kotas kotas/tetration.py
Last active Jan 2, 2016

Embed
What would you like to do?
# -*- coding: utf-8 -*-
from itertools import repeat
foldr = lambda f, xs: reduce(lambda x, y: f(y, x), xs)
applyn = lambda f: lambda n, times: foldr(f, repeat(n, times))
towerN = lambda n: pow if n == 1 else applyn(towerN(n - 1))
tower1 = towerN(1)
tower2 = towerN(2)
tower3 = towerN(3)
print "3^2 = %d" % tower1(3, 2) # 3^2 = 3*3 = 9
print "3^3 = %d" % tower1(3, 3) # 3^3 = 3*(3*3) = 27
print "3^^2 = %d" % tower2(3, 2) # 3^^2 = 3^3 = 27
print "3^^3 = %d" % tower2(3, 3) # 3^^3 = 3^(3^3) = 7625597484987
print "4^^3 = %d" % tower2(4, 3) # 4^^3 = 4^(4^4) = 13407807929...9006084096
print "5^^3 = %d" % tower2(5, 3) # 5^^3 = 5^(5^5) = 19110125979...0908203125
print "3^^^2 = %d" % tower3(3, 2) # 3^^^2 = 3^^3 = 7625597484987
print "3^^^3 = %d" % tower3(3, 3) # 3^^^3 = 3^^(3^^3) = \(^o^)/オワラナイ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.