# How to Solve it by Computer #3

Problem 2.3.5: Develop an algorithm to compute the sums for the first n terms of the following series:

I want to generalize a function to create all these sums. Also there’s one from

Problem 2.3.8: Develop an algorithm to compute the sum of the first n terms of the series

which should also be solved by the function.

One can easily see that all follow the general pattern on `s = a_1 + a_2 + ...` aka. a sum. The only thing that changes is the value of a_i. Let’s look at the individual patterns (I call 2.3.8 (e)):

Let’s take a look at (d) first. It doesn’t follow the ‘increment’ formula so easy. However, we can see that it follows an other formula: 1/1 + 1/2 + 1/3 which is inversed. That is, we generally need the previous value to create the next one.

When we take a look at (e) we can see, that it also depends on the previous value. 1, -3, 5, -7. Generally it adds two but changes the sign. Now we can extend our table.

I just want to explain quickly how I got the increments for (d) and (e). An example is always good.

For 1/2 we take the inverse which is (1 / (1/2)) = 2, add one which makes it 3 and take the inverse, which makes it 1/3. Done.
For (e) we add 2 to the absolute value (abs(-2) = 2), which is trivial and multiply it by -1 which just changes the sign. Example: 2 * -1 = -2, and -3 * -1 = 3.

Now, let’s write some code. I’m currently learning Clojure, so that’s my choice.

Clojure has some cool functions for doing that which I will use. Let’s start with the first series. The function is pretty straight forward:

It takes a number and adds one. Looks good. One noticeable thing is that we also have the `start` attribute. And when you think about it it’s the same as the previous for the first call.

Here’s my function `return-series` which actually returns the series of terms. It’s evaluated lazily therefore it isn’t that problematic on the resources. Also it helps debugging the software in case something breaks.

Here’s an example with the `add-one` function:

I need the `take` function because otherwise the `return-series` function creates an infinite series. Here are the functions for each series:

Which return the sequence like we wanted:

Now that we verified that the terms look correct, we can call `get-sum`:

It works.

This site uses Akismet to reduce spam. Learn how your comment data is processed.