[Programming] C language cyclic structure computing π value,

Source: Internet
Author: User

[Programming] C language cyclic structure computing π value,

Formula:

 

Analysis: first, the denominator of an item with a positive coefficient is 4n-3 (n is the number of items with a positive number), and the denominator of a negative item is 4n-1 (n is the number of items with a negative number ), that is, the rule of denominator change is 1, 3, 5, 7... the denominator of n is 2n-1, and the denominator of 10,000th is 2*10000-1.

 

Code:

# Include <stdio. h> int main () {// calculate the π value int I; int z; double f; double p = 0; for (I = 1; I <= 100000000; I ++) {// modulo z = I % 2; // each f = (double) 1/(2 * i-1 ); // when z is equal to 0, It is a negative value. if (z = 0) {f =-1 * f;} // Add p + = f ;} // The final π value p = p * 4; printf ("% lf", p );}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.