26 lines
629 B
Plaintext
26 lines
629 B
Plaintext
func calculate_pi(terms: i32): f64 {
|
|
let sum: f64 = 0;
|
|
for (let i: i32 = 0; i < terms; i++) {
|
|
let term: f64 = 1.0 / (2 * i + 1);
|
|
if (i % 2 == 0) {
|
|
sum = sum + term;
|
|
} else {
|
|
sum = sum - term;
|
|
}
|
|
if (i % 100000 == 0) {
|
|
|
|
print(term);
|
|
}
|
|
}
|
|
return sum * 4; // 确保乘以4得到正确π值
|
|
}
|
|
|
|
func main(): i32 {
|
|
let terms: i32 = 1000000;
|
|
let pi: f64 = calculate_pi(terms);
|
|
|
|
let output: string = "Calculated π: " + pi.toString() + " (using " + terms.toString() + " terms)";
|
|
print(output);
|
|
|
|
return 0;
|
|
} |