Basic recursive method - factorial

I do recursion, and I do not understand why this method does not work. Any ideas?

public void fact() { fact(5); } public int fact(int n) { if(n == 1){ return 1; } return n * (fact(n-1)); } } 

thanks

+4
source share
8 answers

It seems that your code is working, but you are not doing anything with the return value, call the method of the fact or fact(5) method inside System.out.println and see what you get.

+12
source

The recursive part is beautiful; you just don't use its return value, which is discarded. Here's the full Java application of your factorial, slightly flinching 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" } } 
+6
source

A simplified version of your code:

 public int fact(int n) { if(n == 1){ return 1; } return n * (fact(n-1)); } 

could be simple:

 public int fact(int n) { return n == 1 ? 1 : n * fact(n - 1); } 

but your code is not mistaken, this is just another style (if you are not used to the ternary operator, keep it as it is). I prefer to use the ternary operator in these cases (note that the code is free from side effects).

+2
source

It works great. You are not assigning anything. Here is a test that proves that it works.

 @Test public void testYourFactorialMethod() { assertEquals(120, fact(5)); } 
+1
source
 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 
+1
source

Try something like this: (Or maybe try 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)); } } 
+1
source
 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 a complete example, check this out.

http://answersz.com/factorial-program-in-java-using-recursion/

0
source

It's wrong to write Fibonacci with recursive methods !!

This is an old famous example of how good / bad Algorism affects any project.

if you write Fibonatcci recursive, to calculate 120 you will need a 36-year 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 name like BigInteger, and you can use it to improve the function

using System; using System.Collections.Generic; using System.Numerics; // link required. 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 as

 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(); } } } 

in which case you can calculate 12000 less than a second. so

Using a recursive method is not always a good idea.

Above code imported from the blog of Wahid Nasiri, who wrote in Persian

-7
source

Source: https://habr.com/ru/post/1309316/


All Articles