I have one baseball and want to completely surround it with others. One way: put six around the center one on a table, then three on top, three on the bottom. With real baseballs, this would be difficult. To see an animation of what I'm talking about, check here: http://www.4dsolutions.net/ocn/graphics/cubanim.gif For a lot more on the mathematics, check here: http://oeis.org/A005901

""" I have one baseball and want to completely surround it with others. One way: put six around the center one on a table, then three on top, three on the bottom. With real baseballs, this would be difficult. To see an animation of what I'm talking about, check here: http://www.4dsolutions.net/ocn/graphics/cubanim.gif For a lot more on the mathematics, check here: http://oeis.org/A005901 """ stage.set_background("mars") sprite = codesters.Sprite("person10") sprite.go_to(0, -100) sprite.set_say_color("white") # for speaking def shell(n): """ input n should be a non-negative integer says how many balls in any layer """ if (not isinstance(n, int)) or n < 0: raise TypeError # signals we're done! if n == 0: return 1 # the central ball return 10 * n * n + 2 # otherwise, if n > 0 # put in a negative number to stop while True: # loop until TypeError is raised by user try: guess = int(sprite.ask("What layer number? (-1 to quit): >")) how_many = shell(guess) sprite.say("That layer has " + str(how_many) + " balls in it.") stage.wait(3) except TypeError: sprite.say("OK! Thanks for playing.") stage.wait(2) break sprite.say("Bye!")
Support