Basic recursive method - factorial
I am practicing recursion and I can't see why this method does not seem t开发者_运维百科o work. Any ideas?
    public void fact()
    {
        fact(5);
    }
    public int fact(int n)
    {
        if(n == 1){
            return 1;
        }
        return n * (fact(n-1));
    }
}
Thanks
Your code seems to work but you are not doing anything with the returned value, put method call fact or fact(5) inside of a System.out.println and see what you get.
The recursion part is fine; you're just not using its return value, which gets discarded. Here's a complete Java application of your factorial code, slightly jazzed-up for educational purposes:
public class Factorial {
    public static String fact(int n) {
        if(n == 1){
            return "1";
        }
        return n + " * " + (fact(n-1)); // what happens if you switch the order?
    }
    public static void main(String[] args) {
        System.out.println(fact(5));
        // prints "5 * 4 * 3 * 2 * 1"
    }
}
A simplified version of your code:
public int fact(int n)
{
    if(n == 1){
        return 1;
    }
    return n * (fact(n-1));
}
could be just:
public int fact(int n)
{
    return n == 1 ? 1 : n * fact(n - 1);
}
but your code is not wrong, this is just another style (if you are not used to ternary operator keep the way it is). I prefer use the ternary operator in these cases (observe that the code is side effect free).
Works fine. You're not assigning it to anything. Here's a test that'll prove it works.
@Test
public void testYourFactorialMethod() {
    assertEquals(120, fact(5));
}
public class Recursive {
    public static void main(String[] argss) {
        System.out.print(fac(3));
    }
    public static int fac(int n) {
        int value = 0;
        if (n == 0) {
            value = 1;
        } else {
            value = n * fac(n - 1);
        }
        return value;
    }
}
// out put 6
Try something like this: (Or maybe try this directly)
public class factorial {
    private static int factorial( int n ){
        if (n > 1)  {
            return n * (factorial(n-1));
        } else {
            return 1;
        }
    }
    public static void main(String[] args) {
        System.out.println(factorial(100));
    }
}
static int factorial(int x) {    
    int result;    
    if (x == 1) {    
        return 1;    
    }    
    // Call the same method with argument x-1    
    result = factorial(x – 1) * x;    
    return result;    
}
For complete example check this
http://answersz.com/factorial-program-in-java-using-recursion/
It is totaly wrong to write Fibonacci with recursive methods!!
It is an old famous example for how a good/bad Algorythm affect any project
if you write Fibonatcci recursive, for calculating 120 you need 36 year toget the result!!!!!!
public static int Fibonacci(int x)
{  // bad fibonacci recursive code
if (x <= 1)
      return 1;
return Fibonacci(x - 1) + Fibonacci(x - 2);
}
in dot net 4.0 there is a new type name BigInteger and you can use it to make a better function
using System; using System.Collections.Generic; using System.Numerics; //needs a ref. to this assembly
namespace Fibonaci
{
 public class CFibonacci
 {
   public static int Fibonacci(int x)
   {
       if (x <= 1)
           return 1;
       return Fibonacci(x - 1) + Fibonacci(x - 2);
   }
   public static IEnumerable<BigInteger> BigFib(Int64 toNumber)
   {
       BigInteger previous = 0;
       BigInteger current = 1;
       for (Int64 y = 1; y <= toNumber; y++)
       {
           var auxiliar = current;
           current += previous;
           previous = auxiliar;
           yield return current;
       }
   }
 }
}
and you can use it like
using System;
using System.Linq;
namespace Fibonaci
{
 class Program
 {
   static void Main()
   {
       foreach (var i in CFibonacci.BigFib(10))
       {
           Console.WriteLine("{0}", i);
       }
       var num = 12000;
       var fib = CFibonacci.BigFib(num).Last();
       Console.WriteLine("fib({0})={1}", num, fib);
       Console.WriteLine("Press a key...");
       Console.ReadKey();
   }
 }
}
and in this case you can calculate 12000 less than a second. so 
Using Recursive methos is not always a good idea
Above code imported from Vahid Nasiri blog whiche wrote in Persian
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论