Fibonacci Sequence-java Programming: Three ways to implement Fibonacci sequence __ programming

Source: Internet
Author: User

Topic Requirements: Write program output Fibonacci number 20 in the console, 5 lines per output

Java programming: Three ways to implement the Fibonacci sequence
One way:

public class Demo2 {
	//define three variable methods public
	static void Main (string[] args) {
		int a = 1, b = 1, c = 0;
		System.out.println ("The first 20 items of the Fibonacci series are:");
		System.out.print (A + "T" + B + "T");
		Because there are two 1, 1 in front, so i<=18 for
		(int i = 1; I <= i++) {
			c = a + B;
			A = b;
			b = C;
			System.out.print (c + "T");
			if ((i + 2)% 5 = 0)
				System.out.println ();
		}
	}

Java programming: Three ways to implement the Fibonacci sequence
The second method:

public class Demo3 {
	//definition array method public
	static void Main (string[] args) {
		int arr[] = new INT[20];
		Arr[0] = arr[1] = 1;
		for (int i = 2; i < arr.length i++) {
			arr[i] = arr[i-1] + arr[i-2];
		}
		System.out.println ("The first 20 items of the Fibonacci series are as follows:");
		for (int i = 0; i < arr.length i++) {
			if (i% 5 = 0)
				System.out.println ();
			System.out.print (Arr[i] + "\ t");}}



Java programming: Three ways to implement the Fibonacci sequence

Its three methods:

public class Demo4 {
	//Use recursive method
	private static int Getfibo (int i) {
		if (i = = 1 | | | i = = 2) return
			1;
  else return
			Getfibo (i-1) + Getfibo (i-2);
	}

	public static void Main (string[] args) {
		System.out.println (the first 20 items of the Fibonacci series are:);
		for (int j = 1; J <= J + +) {
			System.out.print (Getfibo (j) + "T");
			if (j% 5 = 0)
				System.out.println ();
		}
	}


The essence of this rabbit problem is the Fibonacci sequence:  has a pair of rabbits, from the 3rd month after the birth of a pair of rabbits each month, small rabbit after the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the number of rabbits each month for how many?, now from variables, arrays, Recursive three angles to solve this puzzle, of course, there are other methods, the same problem with a variety of different ideas to think about solving, but also to the comprehensive use of knowledge exercise bar.


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.