gpt4 book ai didi

c++ - 如何找到以 3.14159 开头的第一个 pi

转载 作者:行者123 更新时间:2023-12-01 14:47:32 25 4
gpt4 key购买 nike

为了确定第一次获得以 3.14159 开头的 pi 需要多少项,我编写了以下程序来计算项为 (pi = 4 - 4/3 + 4/5 - 4/7 + ...) .

我的问题是结果我达到了 146063 个术语,但是当我检查时,有很多 pi 在此之前以类似的方式开始。

//piEstimation.cpp
//estima mathematical pi and detrmin when
//to get a value beganing with 3.14159

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main(){
//initialize vars
double denominator{1.0};
double pi{0};
string piString;
double desiredPi;
int terms;
int firstDesiredTerm;

//format output floating point numbers to show 10 digits after
// decimal poin
cout << setprecision (10) <<fixed;

for (terms = 1; ; terms++){
if(0 == terms % 2){ //if term is even
pi -= 4/denominator;
}
else{ //if term is odd
pi += 4/denominator;
}

// draw table
cout << terms << "\t" << pi << endl;

//determin first time the pi begains with 3.14159
piString = to_string(pi).substr(0,7);
if(piString == "3.14159"){
firstDesiredTerm = terms;
desiredPi = pi;
break;
}
denominator += 2;
}//end for

cout << "The first time that pi value begans with 3.14159 "
<< "the number of terms are " << firstDesiredTerm << " and pi value is "<< desiredPi <<endl;
}//end main

最佳答案

号码x如果 x >= 3.14159 && x < 3.1416,则以 3.14159 开头.不需要使用字符串和比较字符。 to_string必须使用某种圆形操作。如果没有字符串,算法会在 136121 步后找到结果

#include <iostream>
#include <iomanip>

int main(){
//initialize vars
double denominator{1.0};
double pi{0};
double desiredPi;
int terms;
int firstDesiredTerm;

//format output floating point numbers to show 10 digits after
// decimal poin
std::cout << std::setprecision (20) << std::fixed;

for (terms = 1; ; terms++){
if(0 == terms % 2){ //if term is even
pi -= 4/denominator;
}
else{ //if term is odd
pi += 4/denominator;
}

// draw table
std::cout << terms << "\t" << pi << std::endl;

if(pi >= 3.14159 && pi < 3.1416){
firstDesiredTerm = terms;
desiredPi = pi;
break;
}
denominator += 2;
}//end for

std::cout << "The first time that pi value begans with 3.14159 "
<< "the number of terms are " << firstDesiredTerm
<< " and pi value is "<< desiredPi << std::endl;
}

输出:
The first time that pi value begans with 3.14159 the number of terms are 136121 and pi value is  3.14159999999478589672

在这里你可以看到如何 to_string舍入结果:
#include <iostream>
#include <iomanip>
#include <string>

int main(){
std::cout << std::setprecision (20) << std::fixed;
std::cout << std::to_string(3.14159999999478589672) << '\n';
}

输出:
3.141600

您可以阅读 cppreference

std::string to_string( double value ); Converts a floating point value to a string with the same content as what std::sprintf(buf, "%f", value) would produce for sufficiently large buf.



您可以阅读 cppreference

f F Precision specifies the exact number of digits to appear after the decimal point character. The default precision is 6



这意味着 std::to_string 6 位后四舍五入。

关于c++ - 如何找到以 3.14159 开头的第一个 pi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62466381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com