Solving Fibonacci sequence problems using recursion and recursion ~ ~ ~

Source: Internet
Author: User

/**
* Use recursion to process Fibonacci sequences
* @param sum
* @param i
* @return
*/
public static int findvalue (int n) {
if (n==1)
{
return 1;
}
if (n==2)
{
return 2;
}
int sum=1;
int pre=1;
for (int i=3;i<=n;i++)
{
int temp=sum;
Sum+=pre;
Pre=temp;
}
return sum;
}

/**
* The Fibonacci sequence is handled in a recursive manner
* @param i
* @return
*/
public static int findValue2 (int i) {
int sum=0;
int pre=1;
if (i==1)
{
return sum;
}
else{
Pre=findvalue (i--);
Sum+=pre;
}
return sum;
}

public static void Main (string[] args) {
int Value=findvalue (30);
int value2=findvalue2 (30);
System.out.println (value);
System.out.println (value2);
}

Solving Fibonacci sequence problems using recursion and recursion ~ ~ ~

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.