Java-Fibonacci (recursive, or not recursive)

Source: Internet
Author: User

No recursion

Package Com.ikoo;public class Norecursion {public    static void Main (string[] args) {        int front = 1;        int center = 1;        int after = 0;        for (int i = 1; i <=; i++) {            if (i = = 1 | | i = = 2) {                System.out.print (1 + "");            } else {after                = front + Center;                Front = center;//The second number as the next sum first number                center = after;//The third number as the second number to be evaluated next time                System.out.print (after + "");            }        }    }}

Recursive

Package Com.ikoo;import Java.util.scanner;public class Fibonaccisequence {public    static void Main (string[] args) { C1/>system.out.print ("Input sequence Length:");        Scanner Scanner = new Scanner (system.in);        int num = Scanner.nextint ();        for (int i = 1; I <= num; i++) {            System.out.print (Getfibonacci (i) + "\ t");            if (i% 5 = = 0) {                System.out.println ();}}    }    /** *     Recursive thought *      * @param i sequence length (also corresponds to the sequence subscript)     * @return The corresponding value on the I position in the series * * Public    static int Getfib Onacci (int i) {        if (i = = 1 | | i = = 2) {            return 1;        }        Return Getfibonacci (i-1) + Getfibonacci (i-2);    }}
Input sequence Length: 1011235813213455

Recursion-Array

Package Com.ikoo;import Java.util.arrays;import Java.util.scanner;public class Fibonacciarray {public    static void Main (string[] args) {        System.out.print ("Input Fibonacci sequence length:");        Scanner Scanner = new Scanner (system.in);        int len = Scanner.nextint ();        int[] arr = new Int[len];        FIBONACCIARRAY.GETFIBONACC (arr);    }    /**     * Recursive thought-array     *      * @param arr */public    static void Getfibonacc (int[] arr) {for        (int i = 0; i < arr.length; i++) {            if (i = = 0 | | i = 1) {                arr[i] = 1;            } else {                arr[i] = arr[i-1] + arr[i-2];}        }        System.out.println (arrays.tostring (arr));}    }
Input Fibonacci sequence Length: 10[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Java-Fibonacci (recursive, or not recursive)

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.