Created
August 22, 2018 22:38
-
-
Save lukakostic/7f04af9e6fba2643e52bb8236716341b to your computer and use it in GitHub Desktop.
Print digits of pi using the Leibniz formula: pi = 4 * (1 -1/3 +1/5 -1/7 ....)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Print digits of pi using the Leibniz formula: pi = 4 * (1 -1/3 +1/5 -1/7 ....) | |
| #include <stdio.h> | |
| #define NumType long double | |
| int main(){ | |
| NumType d = 1.0; | |
| NumType f = 0.0; | |
| NumType pi = 0.0; | |
| NumType dotRemover = 30000000000000000.0; | |
| for (long long i = 0; (i + 10000000000000000) > 0; i++) | |
| { | |
| f = (NumType)1.0 / (((NumType)3.0) + ((NumType)i) * ((NumType)2.0)); | |
| if (i % 2 == 0) | |
| d -= f; | |
| else | |
| d += f; | |
| if (i % 100000000 == 100) | |
| { | |
| pi = ((NumType)40000000000000000.0) * d; | |
| pi = pi - dotRemover; | |
| printf("3.%Lf\n", pi); | |
| } | |
| } | |
| while (2>0) {} | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment