Skip to content

bhaskar-anil/recursion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

recursion

by defination: Recursion is the repeated application of a recursive procedure or definition.

To understand the recursion we can take an example as I gave answer on stackoverflow here Suppose you want to know what is eligibility to become a prime minister of US, which is:

You have to be national citizen of US, for which here is the rule:

  • You are a natural born of US (or)
  • Your parents are national citizen of US

Now, first option straight forward makes you citizen of US, or same whole thing applies to your parents, which may again check for their parents ..and so on.

So there will be a straight forward answer, which is here 1st option. That is called Base Case. And in 2nd option you might apply same process of checking citizenship for your parents.

Here is a very simple pseudo code that can explain recursion:

isEligibleForPrimeMinister checkCitizenship(You, yourParents){
            Base Case {returns true(That you are US citizen)}
            checkCitizenship(yourParents, YourGrandParents)
          }

I have written some very basic examples that uses recursion: If you learning it from scratch, I suggest try to write it by yourself then look the code to take a hint, don't memorize the code. Find simple code for these in Main.java

  1. Factorial
  2. Multiply two numbers using recursion
  3. LCM
  4. Sum of digits of a number
  5. Power of a number
  6. GCD
  7. Prime Number

PS: I am no expert, I would more than happy if you fork and suggest me an improvement in code in terms of better time complexity of code.

About

Example of recursive functions using Java.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages