1. P S D S - R E C U R S I O N
It Recurs.
By - Kanan,Kirti & Sumit
2. The process in which a function calls itself directly or
indirectly is called recursion and the corresponding function
is called as recursive function.
Using recursive algorithm, certain problems can be solved quite easily.
The function containing recursion is called recursive function, at the end
this is a great tool in the hand of the programmers to code some
problems in a lot easier and efficient way.
In the recursive program, the solution to the base
case is provided and the solution of the bigger problem is
expressed in terms of smaller problems.
The idea is to represent a problem in terms of one or more smaller
problems, and add one or more base conditions that stop the recursion.
For example, we compute factorial n if we know factorial of (n-1).
The base case for factorial would be n = 0. We return 1 when n = 0.
WHAT IS RECURSION ?
3. Property Recursion Iteration
Definition Function calls itself. A set of instructions repeatedly executed.
Application For functions. For loops.
Termination Through base case, where there will be no function call.
When the termination condition for the
iterator ceases to be satisfied.
Usage
Used when code size needs to be small, and time
complexity is not an issue.
Used when time complexity needs to be
balanced against an expanded code size.
Code Size Smaller code size Larger Code Size.
Time Complexity Very high(generally exponential) time complexity.
Relatively lower time complexity
(generally polynomial-logarithmic).
RECURSION VS ITERATION
4. The program ‘Sum of digits’ uses recursion and counts the
sum of first N natural number.
Consider a function existed as sum and if the user pass n
to it, it will return the sum of the number and now that we
have considered that already a function is made so whenever
we will call it, it will give the result as in this code.
The user enters the Nth number as the input, the
program then calculates the sum of first N numbers using recursion
and then displays the final result.
And when the n is not equal to 0, there is no recursive call.
This returns sum of digits ultimately to the main function.
SUM TILL ‘N’ – ALGORITHM
6. For finding the sum of digits, we need to find all digits and sum all of them.
Finding sum of digits includes three basic steps :
1. Find last digit of number using modular division by 10.
2. Add the last digit found above to sum variable.
3. Remove last digit from given number by dividing it by 10.
Algorithm :
1. Get the number
2. Declare a variable to store the sum and set it to 0.
3. Repeat the next two steps, till the number is not 0.
4. Get the rightmost digit of the number with help of the remainder
‘%’ operator by dividing it by 10 and add it to sum.
5. Divide the number by 10 with help of '/' operator to remove
the rightmost digit.
6. Print or return the sum
SUM OF DIGITS – ALGORITHM
8. A Prime number is a number that is divisible only by itself and 1.
For example – 3, 5, 7, 11 etc.
To check if a number is divisible only by 1 and itself, we put every number
greater than 1 and less than the number itself in place of the divisor,
and if we find that the number is divisible by any of them, we break the
loop, and return false, otherwise it is true.
As the above method will not fall under the time constraints, we use
Recursion, in recursion, we check the base cases every single time,
until our condition falls into it, otherwise we increase the value of our divisor
which obviously starts from 2.
Check Prime Or Not – ALGORITHM