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.

89
    89
    Your Cart
    Atlas of Cardiac Innervation
    1 X د.إ542.00 = د.إ542.00
    Dentist Joke Book
    1 X د.إ171.00 = د.إ171.00
    Essentials of Health Policy and Law
    1 X د.إ534.00 = د.إ534.00
    Cybersecurity for eHealth
    1 X د.إ90.00 = د.إ90.00
    Medical Regulatory Affairs
    1 X د.إ90.00 = د.إ90.00
    Anesthesia and the Fetus
    1 X د.إ230.00 = د.إ230.00
    Healthcare Security
    1 X د.إ90.00 = د.إ90.00
    Manganese in Health and Disease
    2 X د.إ200.00 = د.إ400.00
    THE FALLOPIAN TUBES By SN Tripathy
    1 X د.إ13,032.89 = د.إ13,032.89
    Digital Health
    1 X د.إ550.00 = د.إ550.00
    Fluids & Electrolytes Made Incredibly Easy 7th Edition
    2 X د.إ10,449.10 = د.إ20,898.20
    Communication in Nursing 9th Edition
    1 X د.إ14,352.35 = د.إ14,352.35
    Care of the New Born 8th Edition
    1 X د.إ8,999.90 = د.إ8,999.90
    Mayo Clinic Guide To Arthritis
    1 X د.إ87.00 = د.إ87.00
    Sensor Networks for Smart Hospitals
    1 X د.إ882.00 = د.إ882.00
    BD Chaurasia’s Human Anatomy 4 Volume Set
    1 X د.إ2,000.00 = د.إ2,000.00
    Mosby's Drug Guide for Nursing Students 14th Edition
    1 X د.إ12,090.72 = د.إ12,090.72
    Health Assessment for Nursing Practice 7th Edition
    2 X د.إ20,808.01 = د.إ41,616.02
    Tuberculosis and Air Travel 2nd Edition
    1 X د.إ100.00 = د.إ100.00
    World War II
    1 X د.إ162.00 = د.إ162.00
    I REMEMBER THAT PRICE
    1 X د.إ36.00 = د.إ36.00
    Cunninghams Manual of Practical Anatomy 3 Vol Set
    1 X د.إ4,200.00 = د.إ4,200.00
    Handbook of Behavior, Food and Nutrition
    1 X د.إ7,356.00 = د.إ7,356.00
    Allergy Frontiers Therapy and Prevention
    1 X د.إ35.00 = د.إ35.00
    Allergy Methods and Protocols 2nd Edition
    1 X د.إ15.00 = د.إ15.00
    Biomedical Engineering for Audiology
    1 X د.إ350.00 = د.إ350.00
    Influenza Pathogenesis and Control Volume I
    1 X د.إ400.00 = د.إ400.00
    Obstetrics: Normal and Problem Pregnancies By Gabbe
    1 X د.إ23,162.75 = د.إ23,162.75
    AANA Advanced Arthroscopy The Knee
    1 X د.إ300.00 = د.إ300.00
    Medical Surgical Nursing 6th Edition
    1 X د.إ8,194.90 = د.إ8,194.90
    Deciding with Children in Pediatrics
    1 X د.إ880.00 = د.إ880.00
    Introduction to Human Anatomy and Physiology 4th Edition
    1 X د.إ15,142.75 = د.إ15,142.75
    Atlas of Cardiac Surgical Techniques 2nd Edition
    1 X د.إ910.00 = د.إ910.00
    Anesthesia for Urologic Surgery
    1 X د.إ250.00 = د.إ250.00
    HOW TO KEEP YOUR TEETHS HEALTHY
    1 X د.إ186.00 = د.إ186.00
    Cyber and Digital Forensic Investigations
    1 X د.إ697.00 = د.إ697.00
    Problem-Based Learning Approach in Microbiology
    1 X د.إ990.00 = د.إ990.00
    Anatomy & Physiology 8th Edition
    1 X د.إ299.00 = د.إ299.00
    Cardiac Problems in Pregnancy 4th Edition
    1 X د.إ175.00 = د.إ175.00
    A Z of Abdominal Radiology
    1 X د.إ20.00 = د.إ20.00
    How to Develop Your Career in Dental Nursing
    1 X د.إ168.00 = د.إ168.00
    Anatomy & Physiology A Complete Introduction
    1 X د.إ120.00 = د.إ120.00
    Where the Forest Meets the Stars
    1 X د.إ53.00 = د.إ53.00

    Add to cart