Created
August 25, 2013 04:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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