0
Can someone explain how to create a method like this? (I’m confused)
The method accepts a positive integer n and returns the approximation of the natural log of 2 by adding up to n terms in the series Example: 1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n Could some clarify this? Am I supposed to add up n terms in the series to get the natural log of 2? Disclaimer: This is for my Java class but I’m completely lost and have researched online but the question does not make sense to me. Any help or pointers would be greatly appreciated.
2 Respuestas
+ 2
here is Java (11+) (not JavaScript) implementation of Boay.JS’s code.
var sum=1.0;
var n=10000;
for(var i=2;i<=n+2;i++){
if(i%2==0){
sum-=(double)1/i;
}else{
sum+=(double)1/i;
}
}
System.out.println(sum);
+ 1
Yes,you do.
And here is a Javascript(not Java) implementation of it.
var sum=1;
var n=10000;
for(var i=2;i<=n+2;i++){
if(i%2==0){
sum-=1/i;
}else{
sum+=1/i;
}
}
console.log(sum);