How to find first pi beginning with 3.14159

Viewed 174

For determining how many terms are required for the first time getting pi that begins with 3.14159 I wrote the following program that calculates terms as (pi = 4 - 4/3 + 4/5 - 4/7 + ...).

My problem is that I reached 146063 terms as the result but when I checked, there are many pis that begin similarly before that.

//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

1 Answers

A number x begins with 3.14159 if x >= 3.14159 && x < 3.1416. There is no need to use strings and compare characters. to_string has to use some kind of round operation. Without the string the algorithm finds the result after 136121 steps

#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;
}

Output:

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

Here you can see how to_string rounds the result:

#include <iostream>
#include <iomanip>
#include <string>

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

Output:

3.141600

You can read on 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.

You can read on cppreference

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

That means that std::to_string rounds after 6 digits.

Related