X274: Recursion Programming Exercise: Cannonballs

Spherical objects, such as cannonballs, can be stacked to form a pyramid with one cannonball at the top, sitting on top of a square composed of four cannonballs, sitting on top of a square composed of nine cannonballs, and so forth.

Given the following recursive function signature, write a recursive function that takes as its argument the height of a pyramid of cannonballs and returns the number of cannonballs it contains.

Examples:

cannonball(2) -> 5

Your Answer:

x
 
1
public int cannonball(int height) {
2
3
4
}
5

Feedback

Your feedback will appear here when you check your answer.