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.

105
    105
    Your Cart
    Cannabis Physiopathology Epidemiology Detection
    1 X د.إ200.00 = د.إ200.00
    A.D.A.M. Student Atlas of Anatomy 2nd Edition
    1 X د.إ164.52 = د.إ164.52
    Placeholder
    Biology of Domestic Animals By Colin G. Scanes
    1 X د.إ220.00 = د.إ220.00
    Pediatric Rheumatology
    1 X د.إ654.00 = د.إ654.00
    Langman's Medical Embryology 14th Edition
    1 X د.إ200.00 = د.إ200.00
    Medical Management of Wildlife Species
    1 X د.إ523.00 = د.إ523.00
    Transfusion Medicine and Hemostasis
    1 X د.إ885.00 = د.إ885.00
    Essential Concepts in Toxicology By Gupta P.K
    1 X د.إ4,500.00 = د.إ4,500.00
    Handbook on Animal-Assisted Therapy
    1 X د.إ587.00 = د.إ587.00
    Long-Term Care Facilities
    1 X د.إ660.00 = د.إ660.00
    Exosome Communication
    1 X د.إ890.00 = د.إ890.00
    Health Effects of Indoor Air Pollution
    1 X د.إ882.00 = د.إ882.00
    Introduction to Audiology 13th Ed
    1 X د.إ300.00 = د.إ300.00
    100 Cases in Paediatrics 2nd Edition
    1 X د.إ20.00 = د.إ20.00
    Basic Geriatric Nursing 6th Edition
    1 X د.إ20.00 = د.إ20.00
    Radiology of Parasitic Diseases A Practical Approach
    1 X د.إ1,003.00 = د.إ1,003.00
    Gastroenterology and Nutritio
    1 X د.إ452.00 = د.إ452.00
    Edexcel GCSE 9 1 Physics Student Book
    1 X د.إ100.00 = د.إ100.00
    Pediatric Neuroradiology
    1 X د.إ654.00 = د.إ654.00
    50 Great Myths of Human Sexuality
    1 X د.إ20.00 = د.إ20.00
    Lecture Notes Clinical Biochemistry 9th Edition
    1 X د.إ200.00 = د.إ200.00
    The Behavioural Biology of Chickens By Siddaiah
    1 X د.إ6,700.00 = د.إ6,700.00
    Clinical Anatomy A Problem Solving Approach 2nd Ed
    1 X د.إ3,700.00 = د.إ3,700.00
    Medical Image and Signal Analysis in Brain Research
    1 X د.إ1,693.00 = د.إ1,693.00
    Anatomy for Diagnostic Imaging
    1 X د.إ270.00 = د.إ270.00
    Anesthesia in Day Care Surgery
    1 X د.إ120.00 = د.إ120.00
    150 ECG Problems 4th Editio By John R Hampton
    2 X د.إ53.00 = د.إ106.00
    Handbook of Imaging in Multiple Sclerosis
    1 X د.إ970.00 = د.إ970.00
    Audiology The Fundamentals 4th Edition
    1 X د.إ200.00 = د.إ200.00
    This Is What An Amazing Dentist looks Like
    1 X د.إ160.00 = د.إ160.00
    A Wise Doctor Once Wrote
    1 X د.إ165.00 = د.إ165.00
    Mosby's Pocket Dictionary
    1 X د.إ90.00 = د.إ90.00
    Sport and Exercise Medicine An Essential Guide
    1 X د.إ975.00 = د.إ975.00
    Magnetic Resonance Brain Imaging
    1 X د.إ608.00 = د.إ608.00
    Nerve Blockade and Interventional Therapy
    1 X د.إ794.00 = د.إ794.00
    AANA Advanced Arthroscopy The Foot and Ankle
    1 X د.إ250.00 = د.إ250.00
    Nursing Interventions Classification (NIC) 7th Edition
    1 X د.إ13,017.12 = د.إ13,017.12
    Advanced Practice in Endocrinology Nursing
    2 X د.إ40.00 = د.إ80.00
    The Wingmen
    1 X د.إ70.00 = د.إ70.00
    The Secret Life of Cows
    1 X د.إ92.00 = د.إ92.00
    Principles of Medical Imaging for Engineers
    1 X د.إ401.00 = د.إ401.00
    100 Cases in General Practice 2nd Edition
    1 X د.إ20.00 = د.إ20.00
    Marvel Greatest Comics
    1 X د.إ111.00 = د.إ111.00
    Biochemistry An Organic Chemistry Approach
    1 X د.إ100.00 = د.إ100.00
    Atlas of Head and Neck Ultrasound
    1 X د.إ100.00 = د.إ100.00
    Deciding with Children in Pediatrics
    1 X د.إ880.00 = د.إ880.00
    Essentials of Health Policy and Law
    1 X د.إ534.00 = د.إ534.00
    250 Cases In Clinical Medicine 4th Edition
    1 X د.إ20.00 = د.إ20.00
    Radiosurgery of the Skull Base A Case-Based Approach
    1 X د.إ1,234.00 = د.إ1,234.00
    Easy way to FCPS Part 2 Obstetrics Gynaecology
    1 X د.إ150.00 = د.إ150.00
    Asthma and Infections Volume 238
    1 X د.إ250.00 = د.إ250.00
    301 Careers in Nursing 3rd Edition
    1 X د.إ20.00 = د.إ20.00
    Encyclopedia of Toxicology 4th Edition
    1 X د.إ15,288.00 = د.إ15,288.00

    Add to cart