Skip to content

Instantly share code, notes, and snippets.

@kotas
Last active January 2, 2016 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotas/8313672 to your computer and use it in GitHub Desktop.
Save kotas/8313672 to your computer and use it in GitHub Desktop.
# -*- 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