🔒 Closed Calculate pi

Status
Not open for further replies.

Bhadz141

Grasshopper
/*

This program calculates pi to 6 decimal places
which it was limited to due to exceeding the time limit.
it uses the earliest known method for finding pi
developed by Madhava of Sangamagrama during the 14th
and 15th century */

public class Pi {
public static double pi = 4.0;
public static boolean action = true;
public static void main(String[] args) {
for(double i = 3; i<=10000000;i++) {
if(i % 2 == 1){
if(action == true){
pi -= 4/i;
action = false;
}else if (action == false){
pi += 4/i;
action = true;
}
}
}
pi = Math.round(pi*1000000.0)/1000000.0;
System.out.println("Pi is equal to " + pi);
}
}
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 645
    Views
  • 2
    Participants
Last reply from:
Path_Finder

Trending Topics

Online now

Members online
1,338
Guests online
3,477
Total visitors
4,815

Forum statistics

Threads
2,305,879
Posts
29,168,309
Members
1,195,477
Latest member
Jomar Gabriel
Back
Top