The method of the Fibonacci sequence is written in two ways: The program ape Thinking and the program designer's thinking.

Source: Internet
Author: User
Use Java to implement the Fibonacci sequence (Fibonacci) public class Test {public int f (int n)//n represents the first number. The program returns its corresponding value {return n>2?f (n-1) +f (n-2): 1;//seems so graceful a program}//the programmer's mind: The above code will be refactored. Make them easier to read. Recommended!!

! public int Fibo (final int pos) {final int num;if (pos>2) num = Fibo (pos-1) +fibo (pos-2); Elsenum=1;return num;} public static void Main (string[] args) {Test t = new Test (); System.out.println (T.F (8));//Call Program Ape Write method Output 21system.out.println (T.fibo (9));//Call Program Designer to write method output 34//1 2 3 4 5 6 7 8 .... Number//1 1 2 3 5 8 13 21 ... corresponding value}}


The method of the Fibonacci sequence is written in two ways: The program ape Thinking and the program designer's thinking.

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.