C language program, iterative method using the Fibonacci number to calculate the Golden Ratio

Source: Internet
Author: User

Fibonacci number: 1, 1, 2, 3, 5, 8, 13 ............... Wait for the program to launch. Let's take a look at the rule.

The golden ratio calculation method, which is equal to the ratio of the last two items of the Fibonacci number. In this program, we make the absolute values of two consecutive proportional differences less than 10 to the power of-12, it is considered accurate enough, the program output and termination.

Basic Program Framework:

# Include <stdio. h> main () {Double X, xnew; // X is the current number, and xnew is the next number. However, because this program calculates the ratio, double different must be used for both of them, ratio, ratio_new, sum; // ratio is the ratio of the previous time and has been calculated. Ratio_new is the proportion just obtained. // Because the two ratios must be compared here and the difference value is less than 10 to the power of 12, both ratios must be used. X = 0; // 0th x new = 1; // 1ratio_new = 0; do {} while (different> = 1e-12 ); // loop printf ("golden ratio = % lf \ n", ratio_new) when the error is too large );}

Complete procedure

# Include <stdio. h> # include <math. h> main () {Double X, xnew; double different, ratio, ratio_new, sum; X = 0; xnew = 1; ratio_new = 0; do {// The following three rows calculate the Fibonacci number. Sum = x + xnew; X = xnew; xnew = sum;
Ratio = ratio_new; // make the old proportion equal to the new proportion ratio_new = x/xnew; // then update the new proportion, ratio and ratio_new will always represent the old and new proportions. X/xnew is the calculated ratio different = FABS (ratio-ratio_new); // determine the difference between the two ratios} while (different> = 1e-12 ); // loop printf ("golden ratio = % lf \ n", ratio_new) when the error is too large );}

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.