Write (on paper) the code for a RECURSIVE function called 'Icm' that: a. Takes two unsigned integers a and b as input, b. Returns an unsigned integer z as output which will be the least common multiple of a and b, c. Does any error checking or throws any exception as may be needed. A least common multiple z is the lowest number which can be fully divided by both inputs a and b without leaving a remainder. Then write the output of your function for the following two cases: (i) a = 97, b = 311 (ii) a = 77777, b = 67890

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

don't want answer from chat gpt.

### Recursive Function for Calculating Least Common Multiple (LCM)

In this exercise, we aim to develop a recursive function `lcm` that:

1. **Takes Two Unsigned Integers as Input**
   - The function will accept two unsigned integers, `a` and `b`, as its arguments.

2. **Returns an Unsigned Integer as Output**
   - The function will return an unsigned integer `z`, which represents the least common multiple (LCM) of `a` and `b`.

3. **Error Handling**
   - The function includes basic error checking to ensure it operates correctly or throws appropriate exceptions if necessary.

#### Definition of Least Common Multiple (LCM)
The least common multiple (LCM) of two integers `a` and `b` is the smallest positive integer `z` that is divisible by both `a` and `b` without leaving any remainder.

### Function Design
Here's the pseudocode for the recursive `lcm` function:

```python
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

def lcm(a, b):
    if a == 0 or b == 0:
        raise ValueError("Inputs must be non-zero.")
    return (a * b) // gcd(a, b)
```

### Explanation
1. **GCD Sub-function**:
   - The `gcd` function calculates the greatest common divisor (GCD) using a recursive approach based on the Euclidean algorithm.

2. **LCM Calculation**:
   - The `lcm` function computes the LCM using the relationship between GCD and LCM:
     \[
     \text{LCM}(a, b) = \frac{a \times b}{\text{GCD}(a, b)}
     \]
   - It performs a check to ensure that neither `a` nor `b` are zero before proceeding.

### Test Cases
Utilize the function to compute the LCM for the following pairs:

1. **Case (i)**:
   - Inputs: `a = 97`, `b = 311`
   - Output: 
     \[
     \text{LCM}(97, 311) = 30167
     \]

2. **Case (ii)**:
   - Inputs: `a = 77777`, `b = 678
Transcribed Image Text:### Recursive Function for Calculating Least Common Multiple (LCM) In this exercise, we aim to develop a recursive function `lcm` that: 1. **Takes Two Unsigned Integers as Input** - The function will accept two unsigned integers, `a` and `b`, as its arguments. 2. **Returns an Unsigned Integer as Output** - The function will return an unsigned integer `z`, which represents the least common multiple (LCM) of `a` and `b`. 3. **Error Handling** - The function includes basic error checking to ensure it operates correctly or throws appropriate exceptions if necessary. #### Definition of Least Common Multiple (LCM) The least common multiple (LCM) of two integers `a` and `b` is the smallest positive integer `z` that is divisible by both `a` and `b` without leaving any remainder. ### Function Design Here's the pseudocode for the recursive `lcm` function: ```python def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) def lcm(a, b): if a == 0 or b == 0: raise ValueError("Inputs must be non-zero.") return (a * b) // gcd(a, b) ``` ### Explanation 1. **GCD Sub-function**: - The `gcd` function calculates the greatest common divisor (GCD) using a recursive approach based on the Euclidean algorithm. 2. **LCM Calculation**: - The `lcm` function computes the LCM using the relationship between GCD and LCM: \[ \text{LCM}(a, b) = \frac{a \times b}{\text{GCD}(a, b)} \] - It performs a check to ensure that neither `a` nor `b` are zero before proceeding. ### Test Cases Utilize the function to compute the LCM for the following pairs: 1. **Case (i)**: - Inputs: `a = 97`, `b = 311` - Output: \[ \text{LCM}(97, 311) = 30167 \] 2. **Case (ii)**: - Inputs: `a = 77777`, `b = 678
Expert Solution
steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Knowledge Booster
Fibonacci algorithm
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education