Java programming: Three ways to implement Fibonacci sequence __ programming

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_85790123010155m8.html

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

Method One: public class fibonacci1{

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");
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 (); }}}

Method Two: public class fibonacci2{

Defining Array methods
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"); }}}

Method Three: public class Fibonacci3 {

Using recursive methods
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 ();
}}

Xiao Kee: beginners java Edge began to contact the Fibonacci sequence problem (1,1,2,3,5,8,13,21 such a series, starting from the third number of each number equal to its previous two numbers added), began to understand, Later, when doing a rabbit-related exercise, I got some ideas (the essence of this rabbit problem is the Fibonacci sequence: There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits each month, the rabbit long to 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 much?), Now from the variable, array, recursive three point of view to solve this puzzle, of course, there are other methods, the same problem with a variety of ideas to think about solving, but also to the comprehensive use of knowledge exercise bar.

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.