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.

46
    46
    Your Cart
    Chemical Technicians
    1 X د.إ340.00 = د.إ340.00
    Molecular Diagnostics
    1 X د.إ650.00 = د.إ650.00
    Pharmaceutical Statistics
    1 X د.إ654.00 = د.إ654.00
    The MD Anderson Surgical Oncology Manual
    1 X د.إ769.00 = د.إ769.00
    Vital Pulp Treatment
    1 X د.إ476.00 = د.إ476.00
    Only the Strongest Women Become Dental Ceramists
    1 X د.إ164.00 = د.إ164.00
    Dentist Appointment Planner
    1 X د.إ193.00 = د.إ193.00
    Veterinary Psychopharmacology
    1 X د.إ491.00 = د.إ491.00
    Early Phase Cancer Immunotherapy
    1 X د.إ879.00 = د.إ879.00
    Salvage Therapy for Prostate Cancer
    1 X د.إ543.00 = د.إ543.00
    Veterinary Head and Neck Imaging
    1 X د.إ846.00 = د.إ846.00
    Vitiligo
    1 X د.إ869.00 = د.إ869.00
    Paediatric Cases in Coloured Skin
    1 X د.إ438.00 = د.إ438.00
    Clinical Arrhythmology and Electrophysiology
    1 X د.إ1,249.00 = د.إ1,249.00
    Hepatitis C Virus Treatment
    1 X د.إ300.00 = د.إ300.00
    Cancer Medicine
    1 X د.إ456.00 = د.إ456.00
    Taccuino della migliore Dentista del mondo
    1 X د.إ164.00 = د.إ164.00
    The Culmination
    1 X د.إ789.00 = د.إ789.00
    A journal for the best dentist ever
    1 X د.إ160.00 = د.إ160.00
    Common Complications in Endodontics
    1 X د.إ403.00 = د.إ403.00
    Anatomy A Photographic Atlas 8th Edition
    1 X د.إ250.00 = د.إ250.00
    R3 Corda for Architects and Developers
    1 X د.إ978.00 = د.إ978.00
    Medical Laboratory Science Review
    1 X د.إ342.00 = د.إ342.00
    The Longevity Diet
    1 X د.إ260.00 = د.إ260.00
    Anatomy Journal
    1 X د.إ175.00 = د.إ175.00
    Service Science
    1 X د.إ780.00 = د.إ780.00
    Physical Aspects of the Human Body
    1 X د.إ517.00 = د.إ517.00
    Breast Cancer
    1 X د.إ990.00 = د.إ990.00
    The Demon of Unrest
    1 X د.إ118.00 = د.إ118.00
    Recursion
    1 X د.إ58.00 = د.إ58.00

    Add to cart