Skip to content

Instantly share code, notes, and snippets.

@mattmahn
Created August 25, 2013 04:10
Show Gist options
  • Save mattmahn/6331943 to your computer and use it in GitHub Desktop.
Save mattmahn/6331943 to your computer and use it in GitHub Desktop.
Calculates the number of balls stacker in the shape of a triangular pyramid given the number of layers
def count_layer(layer):
""" Returns the number of balls in a layer """
if layer == 0:
return 0
else:
return count_layer(layer-1) + layer
def count_pyramid(numLayers):
currentLayer = 0
numBalls = 0
while currentLayer <= numLayers:
numBalls += count_layer(currentLayer)
currentLayer += 1
return numBalls
foo = int(raw_input("Enter number of layers: "))
print "There are " , count_pyramid(foo) , " balls in the triangular pyramid"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment