计算(pi*pi)/6=1+1/(2*2)+1/(3*3)+…+1/(n*n)关于pi的近似值
时间: 2023-12-18 15:28:11 浏览: 91
PI近似值计算
以下是两种计算(pi*pi)/6=1+1/(2*2)+1/(3*3)+…+1/(n*n)关于pi的近似值的方法:
1. Java实现:
```java
public class PiApproximation {
public static double calculatePi(int n) {
double s = 0.0;
for (int i = 1; i <= n; i++) {
s += 1.0 / (i * i);
}
return Math.sqrt(6 * s);
}
public static void main(String[] args) {
int n = 1000000;
double pi = calculatePi(n);
System.out.println("The approximation of pi is: " + pi);
}
}
```
2. C语言实现:
```c
#include <stdio.h>
#include <math.h>
double calculatePi(int n) {
double s = 0.0;
for (int i = 1; i <= n; i++) {
s += 1.0 / (i * i);
}
return sqrt(6 * s);
}
int main() {
int n = 1000000;
double pi = calculatePi(n);
printf("The approximation of pi is: %lf\n", pi);
return 0;
}
```
阅读全文