Java Loop output Fibonacci sequence

Source: Internet
Author: User

Fiji Retracement--for Baidu Encyclopedia

Fibonacci series (Fibonacci sequence), also known as the Golden Section series, because the mathematician Leonardo's Fibonacci (Leonardoda Fibonacci) to the rabbit breeding as an example of the introduction, so called "Rabbit series", refers to such a series: 1, 1, 2, 3, 5, 8, 13, 21, 、...... In mathematics, the Fibonacci sequence is defined as follows: F (1) =1,f (2) =1, f (n) =f (n-1) +f (n-2) in the fields of modern physics, quasi-crystal structure, chemistry and so on, the Fibonacci sequence has a direct application, Since 1963, the American Mathematical Council has published a mathematical magazine in the name of the Fibonacci series Quarterly, which is devoted to the research.

Loop implementation
public static void main(String[] args) {        // TODO Auto-generated method stub        int n=0;        Scanner sc=new Scanner(System.in);        int x1=1,x2=1;        int temp=x1;        while(true){           n=sc.nextInt();           x1=1;           x2=1;            for(int i=1;i<n;i++) {                temp=x2;                x2=sum(x1,x2);                x1=temp;            }            System.out.println("结果:"+temp);        }                    }    public static int sum(int x1,int x2) {                return x1+x2;    }
Results

Java Loop output Fibonacci sequence

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.