Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Apple Xcode
- #include <stdio.h>
- int main (int argc, const char * argv[]) {
- /* aproximacao do numero pi baseada na serie de Leibniz (1974)
- pi/4 = 1/1 - 1/3 + 1/5 - 1/7 + 1/9 - ... (-1)^(n+1) / (2n-1)
- o termo corrente da serie: (-1)^(k+1) / (2k-1)
- n = 1: 4.0; n = 2: 2.6(6); ...; n = 1000: 3.140593 */
- int the_n = 1;
- int the_signal = 1;
- int i = 1;
- float the_pi4 = 0.0;
- printf("n (n>=1): ");
- scanf("%i", &the_n);
- if (the_n >= 1) {
- while (i <= the_n) {
- the_pi4 = the_pi4 + the_signal / (float)(2 * i - 1); // acumula o termo corrente
- the_signal = -the_signal; // troca sinal
- i++;
- }
- printf("pi = %f\n", 4 * the_pi4);
- } else {
- printf("n >= 1\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement