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.

87
    87
    Your Cart
    Fluids & Electrolytes Made Incredibly Easy 7th Edition
    1 X د.إ10,449.10 = د.إ10,449.10
    Environmental Policy and Public Health
    1 X د.إ90.00 = د.إ90.00
    Pediatric Dysphagia A Multidisciplinary Approach
    1 X د.إ933.00 = د.إ933.00
    AANA Advanced Arthroscopy The Hip
    1 X د.إ250.00 = د.إ250.00
    Disney: 365 Stories
    1 X د.إ199.00 = د.إ199.00
    Anatomy & Physiology 8th Edition
    1 X د.إ299.00 = د.إ299.00
    Abrams Clin Drug Therapy 11 Edition
    1 X د.إ19,449.01 = د.إ19,449.01
    Paddington’s Suitcase
    2 X د.إ112.00 = د.إ224.00
    Foundations of Adult Nursing 2nd Edition
    1 X د.إ9,297.52 = د.إ9,297.52
    Anesthesia in High Risk Patients
    1 X د.إ210.00 = د.إ210.00
    Abnormal Psychology 10th Edition
    1 X د.إ35.00 = د.إ35.00
    Cancer Regional Therapy
    1 X د.إ556.00 = د.إ556.00
    Diagnostic Imaging Genitourinary
    1 X د.إ2,104.00 = د.إ2,104.00
    Safe Water in Healthcare
    1 X د.إ735.00 = د.إ735.00
    Cardiac Imaging Cases
    1 X د.إ250.00 = د.إ250.00
    The Care and Keeping of You 1
    1 X د.إ44.00 = د.إ44.00
    Advances in Psoriasis
    1 X د.إ642.00 = د.إ642.00
    Anesthesia and Analgesia in Dermatologic Surgery
    1 X د.إ200.00 = د.إ200.00
    I'm Friends With The Tooth Fairy notebook
    1 X د.إ157.00 = د.إ157.00
    Eureka: Obstetrics & Gynaecology By Hannah Kither
    1 X د.إ7,761.50 = د.إ7,761.50
    Nursing Today: Transition and Trends 10th Edition
    1 X د.إ16,347.66 = د.إ16,347.66
    Clinical Pharmacy and Therapeutics 6th Edition
    1 X د.إ180.00 = د.إ180.00
    Navigating Organized Urology A Practical Guide
    1 X د.إ401.00 = د.إ401.00
    Business By The Book
    1 X د.إ65.00 = د.إ65.00
    Thieme Clinical Companions Ultrasound
    1 X د.إ175.00 = د.إ175.00
    HarperOne Younge
    1 X د.إ124.00 = د.إ124.00
    Development of New MDR Tuberculosis Drugs
    1 X د.إ100.00 = د.إ100.00
    Profit Pillars
    1 X د.إ109.00 = د.إ109.00
    Go Floss Yourself
    1 X د.إ190.00 = د.إ190.00
    Medical Pharmacology and Therapeutics 5th Edition
    1 X د.إ15,520.83 = د.إ15,520.83
    Acute Myeloid Leukemia in Children
    1 X د.إ765.00 = د.إ765.00
    The Behavioural Biology of Chickens By Siddaiah
    1 X د.إ6,700.00 = د.إ6,700.00
    The Art of Strategy
    1 X د.إ75.00 = د.إ75.00
    Funny Dental Receptionist Gifts
    1 X د.إ165.00 = د.إ165.00
    A Window on Surgery and Orthodontics
    1 X د.إ20.00 = د.إ20.00
    Infectious Disease Epidemiology
    1 X د.إ200.00 = د.إ200.00
    Knowledge Encyclopedia Science
    1 X د.إ102.00 = د.إ102.00
    The Physician Assistant Student's Guide
    1 X د.إ456.00 = د.إ456.00
    The Student's Guide to Becoming a Midwife 2nd Edition
    1 X د.إ8,563.10 = د.إ8,563.10
    Neglected Tropical Diseases - East Asia
    1 X د.إ668.00 = د.إ668.00
    Milady Professional Educator
    1 X د.إ890.00 = د.إ890.00
    Health Psychology
    1 X د.إ990.00 = د.إ990.00
    The Ovary
    1 X د.إ987.00 = د.إ987.00
    Practical Pulmonary Pathology A Diagnostic Approach
    1 X د.إ1,852.00 = د.إ1,852.00
    Anesthesia Complications in the Dental Office
    1 X د.إ25.00 = د.إ25.00
    Essentials Of Public Health Biology
    1 X د.إ890.00 = د.إ890.00
    Anthropology of Violent Death
    1 X د.إ702.00 = د.إ702.00
    100 Questions & Answers About Your Childs Asthma
    1 X د.إ100.00 = د.إ100.00
    Station Eleven
    1 X د.إ114.00 = د.إ114.00

    Add to cart