-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 59e81ce
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package pi; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
public class MainPi { | ||
|
||
public static void main(String[] args) throws NumberFormatException, IOException { | ||
|
||
/*manual precision*/ | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | ||
System.out.print("Enter number of time to do the sum: "); | ||
long nbRepetition = Long.parseLong(reader.readLine()); | ||
|
||
/*Max precision:*/ | ||
//long nbRepetition = Long.MAX_VALUE; | ||
|
||
|
||
|
||
//the taylor series | ||
Double pi = 0.0 , temp = 0.0 ; | ||
for(long i = 0; i < nbRepetition; i++) { | ||
//temporary variable | ||
temp = (Math.pow((-1), i)) / ((2*i)+1); | ||
//add previous temporary variable to actual sum | ||
pi = pi + temp; | ||
} | ||
// ***4*** arctanx x = 1 = Pi | ||
pi = pi*4.0; | ||
|
||
|
||
|
||
System.out.println("\n\nAproximation de Pi: \n\n" + pi); | ||
|
||
//difference with Pi from Eclipse -- Math.PI | ||
System.out.println(pi - Math.PI); | ||
} | ||
} |