/**
* {@code n的阶乘}
* @author Administrator
*
*/
public class factorial {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(functionq(4));
System.out.println(functionw(10));
triangle(5);
System.out.println(factorial(10));
for(int i=0;i<=20;i++)
System.out.print(Fibonacci(i)+" ");
}
/**
* 递归调用来求N的阶乘
*/
public static int functionq(int n){
int sum = 0;
if(n<=1)
sum=1;
else if(n>1)
sum=n*functionq(n-1);
return sum;
}
/**
* 非递归求N的阶乘
*/
public static int functionw(int m){
int sum = 1;
if (m < 0)
return 0;
else if(m ==1)
return 1;
else if (m > 1){
for (int i = 2; i <= m; i++)
sum = sum * i;
return sum;
}
return sum;
}
/**
* 输出三角形
*/
public static void triangle(int m){
for(int i=1;i<=m;i++){
for(int z=1;z<=m-i;z++){
System.out.print(" ");
}
for(int j=1;j<=2*i-1;j++){
System.out.print("*");
}
System.out.println();
}
}
/**
* 递归N!
* 1 n=0
* n!={
* n*(n-1)! n>0
*/
public static int factorial(int n){
if(n==0)
return 1;
return n*factorial(n-1);
}
/**
* Fibonacci数列
* 1,1,2,3,5,8,13,21,34,55,89……
* 1 n=0
* F(n)={ 1 n=1
* F(n-1)+F(n-2) n>1
*/
public static int Fibonacci(int n){
if(n<=1)
return 1;
return Fibonacci(n-1)+Fibonacci(n-2);
}
}
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(functionq(4));
System.out.println(functionw(10));
triangle(5);
System.out.println(factorial(10));
for(int i=0;i<=20;i++)
System.out.print(Fibonacci(i)+" ");
}
/**
* 递归调用来求N的阶乘
*/
public static int functionq(int n){
int sum = 0;
if(n<=1)
sum=1;
else if(n>1)
sum=n*functionq(n-1);
return sum;
}
/**
* 非递归求N的阶乘
*/
public static int functionw(int m){
int sum = 1;
if (m < 0)
return 0;
else if(m ==1)
return 1;
else if (m > 1){
for (int i = 2; i <= m; i++)
sum = sum * i;
return sum;
}
return sum;
}
/**
* 输出三角形
*/
public static void triangle(int m){
for(int i=1;i<=m;i++){
for(int z=1;z<=m-i;z++){
System.out.print(" ");
}
for(int j=1;j<=2*i-1;j++){
System.out.print("*");
}
System.out.println();
}
}
/**
* 递归N!
* 1 n=0
* n!={
* n*(n-1)! n>0
*/
public static int factorial(int n){
if(n==0)
return 1;
return n*factorial(n-1);
}
/**
* Fibonacci数列
* 1,1,2,3,5,8,13,21,34,55,89……
* 1 n=0
* F(n)={ 1 n=1
* F(n-1)+F(n-2) n>1
*/
public static int Fibonacci(int n){
if(n<=1)
return 1;
return Fibonacci(n-1)+Fibonacci(n-2);
}
}