Reply To: Recursion help – beginner

Home Forums Basic Erlang Recursion help – beginner Reply To: Recursion help – beginner


Tomasz Kowal

The concept is simple, but it requires a little bit of mathematical thinking.
You have to use two things:

1. In your formula every part is smaller than previous one:

1/3 > 1/5 > 1/7 > … > 1/(2k + 1) > 1/(2(k+1) + 1) …

2. You alternate between + and – signs. When you add 1/5, you are sure, that even all the rest of terms in the sequence will not change the absolute value more than 1/7, because you substract 1/7, but then add 1/9 to compensate and so on, and so forth.

So, if you take the 4 into the formula, you get: 4 – 4/3 + 4/5 – 4/7 … (+/-) 4/(2k+1)

“Calculate to n decimal places” means, that you need to stop when you are sure, that the digit at nth position will not change. When is that? It is, when you take the pi from previous iteration and current iteration and see that the value changed less than 1/n^10. Next iteration will change it even less, so you can skip it.

It happens, that the change in absolute value is actualy 4/(2k+1). So you don’t have to remember the value of pi from previous iteration.