[plt-scheme] errata HTDP 23.1

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Wed Jul 19 21:44:28 EDT 2006

On Thu, 20 Jul 2006, wooks . wrote:

> The text states that summing the first 10 even numbers should give 90 - 
> the code provided gives 110.

Hi Wooks,

Are you sure?  Just from doing this by hand, without the computer:

     sum    (2 * i)

     = 2 * sum    i

The second term in the product, the arithmetic series, is just the gauss 
sum (n * (n+1) / 2).  Knowing this, we get a closed form:

     = 2 * 9(9+1) / 2
     = 90

The sum of the first ten even numbers has to be 90, if we count zero as 
the first even number.

Hmmm... Oh, I see now what you're talking about!  The problem is more that 
the comment in the contract needs the correction, rather than the code. 
It says:

     ;; series-even : N  ->  number
     ;; to sum up the first n even numbers

but it should really be:

     ;; to sum up the first (n+1) even numbers

because (series-even 1) adds the first and second even numbers together.

Best of wishes!

Posted on the users mailing list.