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.

103
    103
    Your Cart
    2022
    1 X د.إ186.00 = د.إ186.00
    AMC Handbook of Multiple Choice Questions
    1 X د.إ200.00 = د.إ200.00
    Intestinal Polyposis Syndromes
    2 X د.إ514.00 = د.إ1,028.00
    The Book of Strange but True Science
    1 X د.إ47.00 = د.إ47.00
    Cardiac Imaging Cases
    1 X د.إ250.00 = د.إ250.00
    Principles and Practice of Surgery
    1 X د.إ452.00 = د.إ452.00
    Obstetrics: Normal and Problem Pregnancies By Gabbe
    1 X د.إ23,162.75 = د.إ23,162.75
    Medical Image and Signal Analysis in Brain Research
    1 X د.إ1,693.00 = د.إ1,693.00
    An Invitation to Health 13th Edition
    1 X د.إ26,018.77 = د.إ26,018.77
    Anatomy & Physiology 8th Edition
    1 X د.إ299.00 = د.إ299.00
    Textbook of Medical Physiology 2nd Edition
    1 X د.إ7,319.00 = د.إ7,319.00
    Play Smart Wipe-Clean Workbook
    2 X د.إ62.00 = د.إ124.00
    Gerontology For The Health Care Professional
    1 X د.إ90.00 = د.إ90.00
    Anatomy for Dental Students 4th Edition
    1 X د.إ20.00 = د.إ20.00
    Fluids & Electrolytes Made Incredibly Easy 7th Edition
    1 X د.إ10,449.10 = د.إ10,449.10
    Botulinum Toxins Fillers and Related Substances
    1 X د.إ977.00 = د.إ977.00
    Practices of Anorectal Surgery
    1 X د.إ869.00 = د.إ869.00
    Ultrasound Guided Liver Surgery An Atlas
    1 X د.إ100.00 = د.إ100.00
    Handbook on the Toxicology of Metals 4th Edition
    1 X د.إ450.00 = د.إ450.00
    Leadership in Healthcare
    1 X د.إ870.00 = د.إ870.00
    Cardiac Imaging A Multimodality Approach
    1 X د.إ250.00 = د.إ250.00
    I REMEMBER THAT PRICE
    2 X د.إ36.00 = د.إ72.00
    Cosmetic Injection Techniques
    1 X د.إ765.00 = د.إ765.00
    Review of Pediatrics By Abhimanyu Aggarwal
    1 X د.إ4,454.38 = د.إ4,454.38
    Case Studies in Infectious Disease
    1 X د.إ175.00 = د.إ175.00
    Abrams Clin Drug Therapy 11 Edition
    1 X د.إ19,449.01 = د.إ19,449.01
    Long-Term Care Facilities
    1 X د.إ660.00 = د.إ660.00
    Blind Spots
    1 X د.إ90.00 = د.إ90.00
    Challenging Cases in Pleural Diseases
    1 X د.إ510.00 = د.إ510.00
    A Textbook of Modern Toxicology 4th Edition
    1 X د.إ200.00 = د.إ200.00
    THE FALLOPIAN TUBES By SN Tripathy
    1 X د.إ13,032.89 = د.إ13,032.89
    Ultrasound Imaging Medical Applications
    1 X د.إ175.00 = د.إ175.00
    Avery's Diseases Of The Newborn By Christine A. Gleason
    1 X د.إ10,407.51 = د.إ10,407.51
    100 Questions & Answers About Asthma 2nd Edition
    1 X د.إ100.00 = د.إ100.00
    Netter's Obstetrics and Gynecology 3rd Edition
    1 X د.إ18,910.67 = د.إ18,910.67
    Future Science & Technology
    1 X د.إ12.00 = د.إ12.00
    See Inside Your Body
    1 X د.إ49.00 = د.إ49.00
    Ultrasound of The Foot and Ankle
    1 X د.إ100.00 = د.إ100.00
    Urine
    1 X د.إ66.00 = د.إ66.00
    Latinx Farmworkers in the Eastern United States
    1 X د.إ180.00 = د.إ180.00
    See You Later, Alligator
    1 X د.إ61.00 = د.إ61.00
    Placeholder
    How to Draw Anything for kids
    1 X د.إ68.00 = د.إ68.00
    Pediatric Dysphagia A Multidisciplinary Approach
    1 X د.إ933.00 = د.إ933.00
    Probiotic Research in Therapeutics
    1 X د.إ646.00 = د.إ646.00
    Biology of Orthodontic Tooth Movement
    2 X د.إ940.00 = د.إ1,880.00
    Timeless
    2 X د.إ212.00 = د.إ424.00
    Knowledge Encyclopedia - Science
    1 X د.إ102.00 = د.إ102.00
    Coffee in Health and Disease Prevention
    1 X د.إ700.00 = د.إ700.00
    Pharmacy Practice and the Law 9th Edition
    1 X د.إ25.00 = د.إ25.00
    Less Medicine, More Health
    1 X د.إ55.00 = د.إ55.00
    Multiscale Multimodal Medical Imaging
    1 X د.إ190.00 = د.إ190.00
    Cardiac Imaging in Electrophysiology
    1 X د.إ300.00 = د.إ300.00
    Cardiac Imaging
    1 X د.إ400.00 = د.إ400.00
    Ultrasound Q&A Review for the Boards
    1 X د.إ100.00 = د.إ100.00
    Love You Forever
    1 X د.إ83.00 = د.إ83.00
    The Mysteries of the Universe
    1 X د.إ126.00 = د.إ126.00
    America's Heroes and History
    1 X د.إ177.00 = د.إ177.00
    Eruption: Instant #1 New York Times Bestseller
    1 X د.إ134.00 = د.إ134.00

    Add to cart