Skip to content
Advertisement

How to compute approximate natural log of 2 n times in java?

The instructions were: 1. Compute the ln of 2, by adding up to n terms in the series. a) You can approximate the natural logarithm of 2 with a series. The more terms you use, the closer you get to the natural logarithm of 2. b) ln 2 = 1 – 1/2 + 1/3 – 1/4 + 1/5 – … 1/n c) You will need to utilize a For Loop to solve this problem d) You will pass only 1 argument, the value of n, for this program. e) You will need to figure out how to change the sign for each consecutive term. This is not difficult, but it will require some thought.

Forgive me if this doesn’t sound very good, I am not the best at English. My code so far is this: even though I’m aware that I’ve done most wrong. I know it is wrong but I can not find out where to start or how to change the sign. I was at first doing this thinking we needed to find the ln of different numbers but later learned it was with input n. EDIT: I believe I know what major part I was doing wrong. I fixed this to the best of my ability. I’m now a bit stuck because I’m playing out the logic in my head and I believe it should work. Instead all it prints out is 1.0 1.0 1.0 1.0 1.0

Can anyone help point out what I am doing wrong?

public class aprox_log {

JavaScript

}

NEW CODE:

JavaScript

}

`

Advertisement

Answer

To find the sum of the alternating harmonic series with n terms, just loop over the integers from 1 to n. Then, add the reciprocal of the current number if it is odd and subtract the reciprocal if it is even.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement