fibonacci数列
package datastructure;
/**
* Recursion. A method can (directly or indirectly) invoke itself. The system
* automatically creates a stack for it.
*
* @author Michelle Min mitchellemin@163.com
*
*/
public class Recursion {
/**
*
* Sum to N. No loop, however a stack is used.
*
* @param paraN
* The given value.
* @return The sum.
*
*/
public static int sumToN(int paraN) {
if (paraN <= 0) {
// Basis
return 0;
}// Of if
return sumToN(paraN - 1) + paraN;
//return值不断叠加
}// Of sumToN
/**
*
* Fibonacci sequence.
*
* @param paraN
* The given value.
* @return The sum.
*
*/
public static int fibonacci(int paraN) {
if (paraN < 0) {
// Invalid value.
return 0;
//这样在数学中似乎有误,fibonacci数列的第一个应该是0,但paraN从0开始
}//Of if
if (paraN <= 1) {
// Basis.
return 1;
}// Of if
return fibonacci(paraN - 1) + fibonacci(paraN - 2);
}// Of fibonacci
/**
*
* The entrance of the program.
*
* @param args
* Not used now.
*
*/
public static void main(String args[]) {
int tempValue = 5;
System.out.println("0 sum to " + tempValue + " = " + sumToN(tempValue));
tempValue = -1;
System.out.println("0 sum to " + tempValue + " = " + sumToN(tempValue));
for (int i = 0; i < 10; i++) {
System.out.println("Fibonacci " + i + ": " + fibonacci(i));
}// Of for i
}// Of main
}// Of class Recursion