forked from MohmedIkram/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi.java
79 lines (72 loc) · 2.21 KB
/
pi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.*;
import java.math.*;
import java.text.*;
// 1/pi= 2root2/9801 k=0 till infinity (4k)!(1103+26390k)/k!^4*396^4k where k=i;
class pi
{
static int odd=1;
public static void main()
{
System.out.println("Calculating PI..");
calc();
}
public static void calc()
{
BigInteger f1;
BigInteger f2;
BigInteger fm;
BigInteger fe;
BigInteger se;
int n=1103;
BigInteger fa;
BigDecimal currentPi=BigDecimal.ZERO;
BigDecimal pi=BigDecimal.ONE;
BigDecimal one=BigDecimal.ONE;
int n2=2;
double n3=Math.sqrt(2.0);
int n4=9801;
BigDecimal prefix=BigDecimal.ONE;
DateFormat df=new SimpleDateFormat("MM/dd/yy HH:mm:ss");
for(int i=0;i<1000;i++)
{
f1=fact(4*i);
f2=fact(i);
fm=BigInteger.valueOf(26390*i);
fe=exp(f2,4);
se=exp(BigInteger.valueOf(396),4*i);
fa=BigInteger.valueOf(n).add(fm);
currentPi=currentPi.add(new BigDecimal(f1.multiply(fa)).divide(new BigDecimal(fe.multiply(se)),new MathContext(100000)));
Date date=new Date();
System.out.println("Iteration: "+i+" at "+df.format(date));
}
prefix=new BigDecimal(n2*n3);
prefix=prefix.divide(new BigDecimal(n4),new MathContext(1000));
currentPi=currentPi.multiply(prefix, new MathContext(1000));
pi=one.divide(currentPi,new MathContext(1000));
System.out.println("PI is: "+pi);
//return;
}
public static BigInteger fact(int a)
{
BigInteger result=new BigInteger("1");
BigInteger smallRes=new BigInteger("1");
long x=a;
if(x==1)
return smallRes;
while(x>1)
{
result=result.multiply(BigInteger.valueOf(x));
x--;
}
return result;
}
public static BigInteger exp(BigInteger a,int b)
{
BigInteger answer=new BigInteger("1");
for(int i=0;i<b;i++)
{
answer=answer.multiply(a);
}
return answer;
}
}