Recursion

د.إ58.00

ISBN 9780593082607 SKU: 978-0593082607 Category:

Description

Recursion is a programming technique where a function calls itself in order to solve a problem. The key concept behind recursion is that a complex problem can be broken down into simpler instances of the same problem. A recursive function typically has two parts:

1. **Base case**: The condition that stops the recursion. It is the simplest case where the function no longer calls itself.
2. **Recursive case**: The part where the function calls itself with modified arguments, gradually approaching the base case.

### Example: Factorial Calculation
A classic example of recursion is the calculation of the factorial of a number. The factorial of a non-negative integer \( n \) is the product of all positive integers less than or equal to \( n \). It is defined as:

\[
n! = n \times (n-1) \times (n-2) \times \dots \times 1
\]

The recursive definition of factorial is:

\[
n! =
\begin{cases}
1 & \text{if } n = 0 \\
n \times (n-1)! & \text{if } n > 0
\end{cases}
\]

### Code Example (Python):
“`python
def factorial(n):
# Base case: when n is 0, return 1
if n == 0:
return 1
# Recursive case: multiply n by factorial of (n-1)
else:
return n * factorial(n – 1)

print(factorial(5)) # Output: 120
“`

### How It Works:
– The base case is when \( n = 0 \), where the factorial is 1.
– For \( n > 0 \), the function calls itself with \( n – 1 \) until it reaches the base case.

### Advantages of Recursion:
1. **Simplicity**: Recursion can make code simpler and more readable, especially for problems that have a naturally recursive structure (like tree traversal or factorials).
2. **Divide and Conquer**: Problems that can be divided into smaller subproblems (like sorting algorithms) can benefit from recursion.

### Disadvantages of Recursion:
1. **Memory Consumption**: Each recursive call adds a new layer to the call stack. If recursion goes too deep, it may lead to a stack overflow.
2. **Efficiency**: Recursion can sometimes be less efficient compared to iterative solutions, especially if not optimized (e.g., with memoization).

### Tail Recursion:
A specific type of recursion, **tail recursion**, occurs when the recursive call is the last operation in the function. Tail recursion can be optimized by the compiler to avoid adding a new stack frame for each call, effectively making it as efficient as iteration.

### Example of Tail Recursion:
“`python
def factorial_tail_recursive(n, accumulator=1):
# Base case: when n is 0, return the accumulated value
if n == 0:
return accumulator
# Recursive case: multiply accumulator by n and pass to the next recursive call
else:
return factorial_tail_recursive(n – 1, accumulator * n)

print(factorial_tail_recursive(5)) # Output: 120
“`

In this example, the recursive call is the last operation, and the result is carried through the `accumulator` argument.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

76
    76
    Your Cart
    Bioprospecting of Ethnomedicinal Plant Resources
    1 X د.إ645.00 = د.إ645.00
    Recognizable Patterns of Human Deformation
    3 X د.إ490.00 = د.إ1,470.00
    Herbal Formulations Phytochemistry and Pharmacognosy
    1 X د.إ1,323.00 = د.إ1,323.00
    Spine Surgery A Case-Based Approach
    3 X د.إ1,337.00 = د.إ4,011.00
    Clinical Anatomy A Problem Solving Approach 2nd Ed
    1 X د.إ3,700.00 = د.إ3,700.00
    Cunninghams Manual of Practical Anatomy 3 Vol Set
    1 X د.إ4,200.00 = د.إ4,200.00
    BD Chaurasia’s Human Anatomy 4 Volume Set
    1 X د.إ2,000.00 = د.إ2,000.00
    The Surgery-First Orthognathic Approach
    1 X د.إ764.00 = د.إ764.00
    Population and Community Health Nursing
    2 X د.إ30.00 = د.إ60.00
    Geriatric Ophthalmology
    1 X د.إ345.00 = د.إ345.00
    Harper Collins India The Wellness Sense
    1 X د.إ30.00 = د.إ30.00
    Surgery for Parkinson's Disease
    1 X د.إ103.00 = د.إ103.00
    Advances in Vision Research Volume II
    1 X د.إ765.00 = د.إ765.00
    Cutting Edge of Ophthalmic Surgery
    2 X د.إ990.00 = د.إ1,980.00
    Managing Health Care Business Strategy
    1 X د.إ50.00 = د.إ50.00
    Cross Sectional Anatomy CT and MRI
    1 X د.إ1,300.00 = د.إ1,300.00
    Super Gut
    2 X د.إ134.00 = د.إ268.00
    Principles and Practice of Lymphedema Surgery
    1 X د.إ572.00 = د.إ572.00
    Druggable Lipid Signaling Pathways
    1 X د.إ877.00 = د.إ877.00
    Maternal Child Nursing Care
    1 X د.إ456.00 = د.إ456.00
    Brain Imaging A Guide for Clinicians
    1 X د.إ1,087.00 = د.إ1,087.00
    Trauma and Miscellaneous Disorders in Retina
    1 X د.إ567.00 = د.إ567.00
    Retinal Degeneration
    1 X د.إ678.00 = د.إ678.00
    Minimally Invasive Aesthetic Surgery
    1 X د.إ1,111.00 = د.إ1,111.00
    Treatment of Non-infectious Uveitis
    1 X د.إ765.00 = د.إ765.00
    Clinical Nursing Skills and Techniques
    2 X د.إ467.00 = د.إ934.00
    Optic Disorders and Visual Field
    1 X د.إ656.00 = د.إ656.00
    Anti Infection Handbook
    1 X د.إ250.00 = د.إ250.00
    Palliative Care Perspectives
    1 X د.إ256.00 = د.إ256.00
    Neuro-Ophthalmology and Neuro-Otology
    1 X د.إ324.00 = د.إ324.00
    The Breastfeeding Atlas
    1 X د.إ270.00 = د.إ270.00
    The Big Book of Bible Difficulties
    1 X د.إ207.00 = د.إ207.00
    Interpersonal Relationships
    1 X د.إ456.00 = د.إ456.00
    S.Chand Mechanical Engineering
    1 X د.إ51.00 = د.إ51.00
    Aesthetic Orthognathic Surgery and Rhinoplasty
    2 X د.إ1,041.00 = د.إ2,082.00
    A Wrinkle in Time
    1 X د.إ91.00 = د.إ91.00
    DK Encyclopedia of Herbal Medicine
    1 X د.إ83.00 = د.إ83.00
    Fast Like a Girl
    1 X د.إ82.00 = د.إ82.00
    Chest CT for Non-Radiologists A Practical Guide
    1 X د.إ534.00 = د.إ534.00
    3D Echocardiography 3rd Edition
    1 X د.إ553.00 = د.إ553.00
    Atlas of Wide-Field Retinal Angiography and Imaging
    1 X د.إ1,028.00 = د.إ1,028.00
    Structures: Or Why Things Don't Fall Down
    1 X د.إ81.00 = د.إ81.00
    1 X د.إ150.00 = د.إ150.00
    Recursion
    1 X د.إ58.00 = د.إ58.00

    Add to cart