Recursive Algorithm -- Obtain the Fibonacci sequence (2)

Source: Internet
Author: User
Import Java. util. created;/*** created by Administrator on 14-5-13. * The improved method for calculating the Fibonacci sequence. Using parameters, the test run time will multiply and decrease the test data n = 40 * the essence of tail recursion is to cache the results of a single computation, passed to the next call, which is equivalent to automatic accumulation. * Tail recursion is a programming technique. A recursive function is a function that calls its own function. * If the result returned by a recursive function is always directly returned, it is called tail recursion. * Recursive functions at the end can be used to convert algorithms into function programming languages. * from the compiler perspective, algorithms can easily be optimized into common loops. * This is because from the computer fundamentals, all cycles are implemented by repeatedly moving to the beginning of the Code. * If there is tail delivery, you only need to stack one stack, because the computer only needs to change the function parameters and then call them again. * Tail recursion is mainly used to optimize tail recursion. For example, in scheme, it is specified that tail recursion must be optimized. * The tail recursion function is very dependent on the specific implementation. */Public class fib1 {public static void main (string [] ARGs) {long starttime = system. currenttimemillis (); // get start time int number = 0; system. out. println ("please give the number:"); commandid = new partition (system. in); string STR = response. nextline (); try {number = integer. parseint (STR);} catch (numberformatexception e) {system. out. println ("incorrect input");} system. out. println (FAC (number, 1, 1); long endtime = system. currenttimemillis (); // obtain the end time system. out. println ("program running time:" + (endtime-starttime) + "Ms");} public static int FAC (INT temp, int F1, int F2) {If (temp <2) {return F1;} else {system. out. println ("FAC (" + (temp-1) + ''+ F2 + ',' + (F1 + F2) +") "); Return FAC (temp-1, F2, f2 + F1 );}}}

 

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.