Recursive algorithm-to find the factorial of N, Super can born rabbit-java to achieve __ algorithm

Source: Internet
Author: User
/** * Recursive algorithm * @author timmy1 * * */public class Recursion {/** * n factorial * @param n */public int factorial (int n) {if (n = 1) ) {return 1;} else{return n*factorial (n-1);} /** * @param Month Month * @return/public int rabbitgrow (int month) {if (Month ==1 | | month = 2) {//First month and second month only A pair of rabbits, by the third month, the rabbit began to breed a pair of rabbits return 1; }else{return Rabbitgrow (month-1) +rabbitgrow (month-2);} public static void Main (string[] args) {recursion recursion = new recursion ();//for (int i = 1;i < 10;i++) {//System The factorial result of Out.println (i+) is: "+recursion.factorial (i));" for (int i = 1;i < 12;i++) {System.out.println (i+) The number of rabbits in the month results in: "+recursion. Rabbitgrow (i)); } } }

Results Print:

January Rabbit Count Results: 1

February Rabbit Count Results: 1

March Rabbit Count Results: 2

April Rabbit Count Results: 3

May Rabbit Count Results: 5

June Rabbit Count Results: 8

July Rabbit count Results: 13

August Rabbit Count Results: 21

September Rabbit count Results: 34

October Rabbit count Results: 55

November Rabbit Count Results: 89

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.