I'm hoping I'm not violating any of the forum rules, I have put some work into this code, but I'm sort of stuck on some exact things lol...
I'm working on a homework assignment and one part of the assignment is to write a method to approximate the value of Pi using the series 4(1 - 1/3 + 1/5 - 1/7 + 1/9 + 1/11 ... - 1/(2i-1) + 1/(2i+1), assuming i is 1000000.
This is my code:
and my return is 3.1415936535887745 when the return needs to be 3.141590653589692, the first 7 digits are right, but beyond its off. I've been recommended to extend the value of i, but the assignment says to assume i is 1000000, so I'm going to assume I can't make i and bigger, any ideas?
Another problem I'm having is approximating the square root of a number (like 10 for example). My professor wants us to write a method to approximate the square root of a value by using the formula nextGuess = (lastGuess + (num / lastGuess)) / 2 and when the difference between nextGuess and lastGuess is less then .00001 you can assume that nextGuess is the approximated square root.
Edit: Fixed my square root problem.
Thanks and sorry about the long post.
I'm working on a homework assignment and one part of the assignment is to write a method to approximate the value of Pi using the series 4(1 - 1/3 + 1/5 - 1/7 + 1/9 + 1/11 ... - 1/(2i-1) + 1/(2i+1), assuming i is 1000000.
This is my code:
Code:
public static double pi()
{
double pi = 0.0;
for(int i = 0; i <= 1000000; i++)
{
pi += 4 * (1.0/(1 + i*2) * ((i % 2 == 0) ? 1 : -1));
}
return pi;
}
Another problem I'm having is approximating the square root of a number (like 10 for example). My professor wants us to write a method to approximate the square root of a value by using the formula nextGuess = (lastGuess + (num / lastGuess)) / 2 and when the difference between nextGuess and lastGuess is less then .00001 you can assume that nextGuess is the approximated square root.
Edit: Fixed my square root problem.
Thanks and sorry about the long post.