File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes recursive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "recursive" Watch "recursive" New topic


abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635

Well, as stated in the description of a recursive definition, a base case and inductive rule were needed. In this case, fact(0)=1 is the base case. This is a logical place to start, seeing as how the n in fact(n) must be larger than or equal to 1 (only positive integers). The other half is the inductive rule of the problem, which in this case is represented by “fact(n)=fact(n-1)*n”. We can see the logic in this, as the factorial of any number, is simply the factorial of the number one smaller than it, times the number you’re finding the factorial of.

In this tutorial , it is said that a recursive function divide to the two part, Base and inductive

Now, look at this function:

In this function, If n>0 is base, and rem=n%10;
s += rem;

Is inductive?

Is it right that said every recursive function divide to the two parts?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

What language is that second function in? It's not Java, it's not C, it looks like some kind of pseudo-code, and it doesn't look complete. What happens in the second function when n == 0 or n < 0? That (the missing part) would be the base case. The inductive case is what's between the { and } of the if-statement; that's the part that does the recursive call.

Recursive functions always need an if-statement in some form. If there is no terminating condition (the base case), you would have an infinite recursive loop, and the program would end with a stack overflow error.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: recursive
It's not a secret anymore!