next up previous
Next: 3 Finding the Zero Up: APS105S Tuesday Quiz 1 Previous: 1 Basic Programming Constructs

2 Recursive Functions

Write a recursive function that takes as input a positive integer N and determines the sum of the squares of the numbers from 1 to N. That is, given N, calculate:

Using this function write a complete Turing program that continuously reads in the value N, and calls the function to calculate the sum of the squares from 1 to N. The program should exit when the input value of N is less than 0.

For example, an input/output sequence would be as follows;

Input:  4
Output: The sum of the squares from 1 to 4 is 30

Input:  6
Output: The sum of the squares from 1 to 6 is 91

Input: -3
Output: <program exits>

Restrictions:

The function you write must be recursive. No marks will be given for a non-recursive implementation.

A solution



Jonathan Rose
Sat Mar 8 17:24:49 EST 1997