Featured Post

Catalan's Constant Algorithm

Brief Theory : Wiki Link 

Code 1 : Basic (Worst)

#include<bits/stdc++.h>

using namespace std;

typedef long long l;

typedef long double f;

int main(){

l t,i,n;

cout<<"No of testcases: ";

cin>>t;

while(t--){

cout<<"Decimal No of precision :";

cin>>n;

n=ceil(sqrt(pow(10,n+1)));

l d=0;

f a=1,b=1,r;

while(d<=n){

d++;

if(d%2)

a=b-1/pow(2*d+1,2);

else b=a+1/pow(2*d+1,2);

}

r=(n%2) ? a:b;

cout<<"Catalan's constant for "<<n<<"th iteration: "<<setprecision(50)<<r<<endl;

}

return 0;

}


Code 2 (Better)

#include<bits/stdc++.h>

using namespace std;

typedef long long l;

typedef long double f;


int main(){

l t,i,n;

cout<<"No of testcases: ";

cin>>t;

while(t--){

cout<<"No of Iteration: ";

cin>>n;

f r=M_PI*log(2+sqrt(3))/8+3/8;

i=1;

f a=0.5,term=0.5,p=0.5;

while(i<=n){

p=p*i/(2*i+1);

term=term*i/(2*i+1);

term=term+p/(2*i+1);

a+=term;

i++;

}

cout<<"Catalan's constant for "<<n<<"th iteration: "<<setprecision(50)<<a<<endl;

}

return 0;

}


Code 3 (Best : Ramanujan's Formula Based Algorithm)

#include<bits/stdc++.h>

using namespace std;

typedef long long l;

typedef long double f;


int main(){

l t,i,n;

cout<<"No of testcases: ";

cin>>t;

while(t--){

cout<<"No of Iteration: ";

cin>>n;

f r=M_PI*log(2+sqrt(3))/8+3/8;

i=1;

 f a=1,term=1,p;

while(i<=n){

term=term*(2*i-1)*2/i;

p=term*pow(2*i+1,2);

a+=1/p;

i++;

}

a=r+a*3/8;

cout<<"Catalan's constant for "<<n<<"th iteration: "<<setprecision(50)<<a<<endl;

}

return 0;

}


Thanks 

Team IIT KGP Study Material 


👉👉 Sales here :

Best offer in Electronics : https://amzn.to/3riZXQm (Amazon)


Buy Books at Discounted Price from @Amazon

👉👉👉  Chemistry Book Suggestions for First Year :

1. Physical Chemistry (For Theory) : https://amzn.to/2QLdXpG (Amazon)

2. Physical Chemistry (For Practical) : https://amzn.to/3d6SU8w (Amazon)

3. Vogel's Qualitative Inorganic Analysis : https://amzn.to/2QJhmW2 (Amazon)

4. Vogel's Qualitative Inorganic & Chemical Analysis Bundle : https://amzn.to/2O32CjQ (Amazon)

5. Inorganic Chemistry (Theory) : https://amzn.to/3tRXeiK (Amazon)

6. Inorganic Chemistry (Theory) :  https://amzn.to/3csd65C (Amazon)

7. Inorganic Chemistry (Theory) :  https://amzn.to/2QBOBKI (Amazon)

8. Shriver Atkins Inorganic Chemistry (Theory) :  https://amzn.to/3w6VzYj (Amazon) 

9. Organic Chemistry (Theory) : https://amzn.to/2Pc1pXY (Amazon)

10. Organic Chemistry (Theory) : https://amzn.to/2PAMWEV (Amazon)

11. A Guidebook to Mechanism in Organic Chemistry : https://amzn.to/31qIH1a (Amazon)


👉👉👉 My favourite and MUST READ Mathematics Books for Olympiad Aspirants : 


1.  Problem Solving Strategy ( Arthur Engel ): https://amzn.to/3tYcVVB (Amazon)

2.  Mathematical Circles : https://amzn.to/3rvhGUZ (Amazon )

3.  Challenge and Thrill of Pre-College Mathematics : https://amzn.to/3fkLXn6 (Amazon)

4.  Functional Equations : https://amzn.to/31o0k1w (Amazon)

5.  Inequalities: An Approach Through Problems : https://amzn.to/3rwmTf8 (Amazon)

6.  Hall and Knight Algebra : https://amzn.to/31qlGeU (Amazon)

7.  INMO Book by Manocha : https://amzn.to/31D6Qlv (Amazon)

8.  Theory of Equations : https://amzn.to/3ruJxEE (Amazon)

9.   An Excursion in Mathematics : https://amzn.to/3u0WGXK (Amazon)

Comments

Search This Blog