NCERT Complete Step-by-Step Solutions

The World of Algorithms

Complete and rigorous mathematical solutions for Exercise Set 11.1, 11.2, 11.3, Think and Reflect, and End-of-Chapter Exercises from NCERT Ganita Manjari Grade 9 (Part II).

Quick Navigation
Fundamental Algorithmic Paradigms
Column Addition Algorithm
Align right-to-left by positional place value. Add column digits and carry. Carry can never exceed $1$ when adding two base-10 digits ($9+9+1 = 19$).
Euclid's & Āryabhaṭa's GCD
$\gcd(m, n) = \gcd(n, m \bmod n)$ for $n > 0$, with base case $\gcd(m, 0) = m$. Reduces problem exponentially faster than linear factor search!
Divisor Pairing Property
If $d \mid n$, then $(n/d) \mid n$. One factor is $\le \sqrt{n}$ and the other is $\ge \sqrt{n}$. Hence checking numbers up to $\lfloor\sqrt{n}\rfloor$ suffices!

Exercise Set 11.1

5 Questions
Exercise 11.1 • Question 1
NCERT
Add two 4-digit numbers using the steps we have written down. Make sure you follow the steps precisely; do not perform any action that is not explicitly mentioned. Are you able to obtain the correct result?
Step-by-Step Solution:
Let us choose two arbitrary 4-digit numbers, for example, $4875$ and $3648$.

Execution of the algorithm steps:
  • Step 1: Write the numbers one below the other aligned at the rightmost column: $$\begin{array}{r@{\quad}cccc} & 4 & 8 & 7 & 5 \\ + & 3 & 6 & 4 & 8 \\ \hline \end{array}$$ Set carry = $0$.
  • Step 2: Add rightmost digits (units): $5 + 8 + 0 = 13$. The units digit is $3$, and new carry is $1$. Write $3$ in units column.
  • Step 3 (Tens column): Add $7 + 4 + 1 = 12$. The units digit is $2$, new carry is $1$. Write $2$ in tens column.
  • Step 3 (Hundreds column): Add $8 + 6 + 1 = 15$. The units digit is $5$, new carry is $1$. Write $5$ in hundreds column.
  • Step 3 (Thousands column): Add $4 + 3 + 1 = 8$. The units digit is $8$, new carry is $0$. Write $8$ in thousands column.
  • Step 4: No more digits remain in the numbers.
  • Step 5: Check final carry. Since carry is $0$, no extra leftmost digit is needed.
The resulting number is $8523$.
Verification: $4875 + 3648 = 8523$.
Yes, following the algorithm steps precisely and mechanically without any extra outside actions yields the correct sum ($8523$).
Exercise 11.1 • Question 2
NCERT
What happens if you add a 5-digit number to a 3-digit number? Do our steps handle this situation correctly?
Step-by-Step Solution:
Let us examine adding a 5-digit number, say $24385$, and a 3-digit number, say $768$.
  • If the algorithm assumes that both numbers always have the same number of digits, a missing digit in the 3-digit number would cause an error when reaching the thousands and ten-thousands columns.
  • However, if we pad the shorter number with leading zeros (or define a missing digit as having value $0$), the algorithm continues seamlessly: $$24385 + 00768$$
  • At column 4 (thousands): $4 + 0 + \text{carry} = 5$.
  • At column 5 (ten-thousands): $2 + 0 + 0 = 2$.
The steps handle this situation correctly provided that the numbers are aligned to the right and any empty position in the shorter number is treated as $0$.
Exercise 11.1 • Question 3
NCERT
Why is it important to align the columns from right to left?
Step-by-Step Solution:
In our Hindu-Arabic decimal positional numeral system:
  • The place value of a digit is determined by its distance from the decimal point (or rightmost units position). The rightmost digit represents $10^0 = 1$ (units), the second rightmost represents $10^1 = 10$ (tens), the third represents $10^2 = 100$ (hundreds), and so on.
  • Mathematical addition requires adding quantities of the same denomination (like terms): units must be added to units, tens to tens, and hundreds to hundreds.
  • If we aligned from left to right, when adding numbers of different lengths (such as $452$ and $31$), the $3$ of $31$ (which represents $3 \times 10$) would be aligned and added to the $4$ of $452$ (which represents $4 \times 100$), leading to mathematically erroneous sums.
Aligning right-to-left ensures that digits with the same positional place value ($10^0, 10^1, 10^2, \dots$) are placed in the same column and added together.
Exercise 11.1 • Question 4
NCERT
In Step 3, why cannot the value of carry be more than 1?
Step-by-Step Solution:
Let us examine the maximum possible sum that can be formed in any single column when adding two base-10 digits:
  • In base-10, the maximum possible value of a single digit is $9$.
  • When adding two numbers, each column contains two digits $d_1 \le 9$ and $d_2 \le 9$, plus an incoming carry $c_{\text{in}}$.
  • Assume by mathematical induction that the incoming carry $c_{\text{in}} \le 1$.
  • The maximum possible column sum is: $$\text{Sum}_{\max} = d_{1,\max} + d_{2,\max} + c_{\text{in},\max} = 9 + 9 + 1 = 19$$
  • The outgoing carry is the tens digit of this sum: $$c_{\text{out}} = \lfloor 19 / 10 \rfloor = 1$$
Since the maximum possible column sum is $19$, the carry to the next column can never be greater than $1$.
Because the maximum sum of two decimal digits plus a carry of $1$ is $9 + 9 + 1 = 19$, which produces a tens digit of at most $1$.
Exercise 11.1 • Question 5
NCERT
What happens if we do not include the fifth step in the algorithm above? Give examples where the algorithm will work correctly and where it will fail to work.
Step-by-Step Solution:
Step 5 states: "If the value of carry is 1, write 1 to the left of the sum." If this step is omitted, any final carry generated by the leftmost column will be lost, truncating the answer.

Example where it works correctly (no final carry):
  • Adding $213 + 142$: Column additions: $3+2=5, 1+4=5, 2+1=3$. Carry after leftmost column is $0$. Output: $355$. Here, Step 5 is not needed, and the answer is completely correct.

Example where it fails to work (final carry lost):
  • Adding $650 + 520$: Units: $0+0=0$. Tens: $5+2=7$. Hundreds: $6+5=11 \implies \text{write } 1, \text{ carry } = 1$. If Step 5 is omitted, the final carry $1$ is never recorded, producing the erroneous result $170$ instead of the correct sum $1170$!
Without Step 5, additions that generate an extra digit overflow (e.g. $650+520=1170$) will fail, while additions that do not overflow (e.g. $213+142=355$) will still work.

Think & Reflect

2 Questions
Think & Reflect • Question 1
NCERT
See if you can complete the argument about grouping by units, tens, hundreds, … to justify why the addition algorithm works.
Step-by-Step Solution:
Let two numbers $A$ and $B$ be expressed in expanded positional form: $$A = a_0 \cdot 10^0 + a_1 \cdot 10^1 + a_2 \cdot 10^2 + \dots + a_k \cdot 10^k$$ $$B = b_0 \cdot 10^0 + b_1 \cdot 10^1 + b_2 \cdot 10^2 + \dots + b_k \cdot 10^k$$ By the associative and commutative properties of real addition: $$A + B = (a_0 + b_0) \cdot 10^0 + (a_1 + b_1) \cdot 10^1 + (a_2 + b_2) \cdot 10^2 + \dots$$ At each position $j$:
  • The sum of digits plus incoming carry $c_j$ can be written by Euclidean division by 10 as: $$a_j + b_j + c_j = 10 \cdot c_{j+1} + s_j$$ where $s_j \in \{0, 1, \dots, 9\}$ is the recorded digit and $c_{j+1} \in \{0, 1\}$ is the carry.
  • Multiplying by $10^j$: $$(a_j + b_j + c_j) \cdot 10^j = s_j \cdot 10^j + c_{j+1} \cdot 10^{j+1}$$
  • The carry term $c_{j+1} \cdot 10^{j+1}$ precisely has the place value $10^{j+1}$ of the next column to the left, which justifies adding it into the next column sum.
Summing across all columns telescopes into the exact decimal representation of $A + B$.
The algorithm is rigorously justified by the distributive law and place-value decomposition in base-10: $10 \text{ units} = 1 \text{ ten}$, $10 \text{ tens} = 1 \text{ hundred}$, etc.
Think & Reflect • Question 2
NCERT
How would you modify the algorithm to add two decimal fractions?
Step-by-Step Solution:
To add two decimal fractions (e.g. $34.56 + 7.892$):
  • Step 1 (Anchor): Instead of aligning rightmost digits, align the numbers by their decimal points vertically.
  • Step 2 (Pad Zeros): Pad the fractional part of the shorter number with trailing zeros so that both numbers have the same number of decimal places (e.g., $34.560$ and $07.892$).
  • Step 3 (Execute Standard Addition): Starting from the rightmost decimal column, perform the standard column addition from right to left, carrying over across columns exactly as before.
  • Step 4 (Decimal Point Placement): Place the decimal point in the final result directly below the aligned decimal points.
  • Step 5: If a final carry remains after the leftmost whole-number digits, write $1$ at the extreme left.
Modify the algorithm by aligning the decimal points vertically, padding with trailing zeros to match fractional lengths, and applying standard column addition right-to-left.

Exercise Set 11.2

3 Questions
Exercise 11.2 • Question 1
NCERT
Suppose $\text{List } 1$ and $\text{List } 2$ are two lists of numbers in increasing order.
(i) Write an algorithm to find elements in $\text{List } 1$ that are not present in $\text{List } 2$.
(ii) Write an algorithm to find elements in $\text{List } 2$ that are not present in $\text{List } 1$.
Step-by-Step Solution:
Since both lists are sorted in increasing order, we can use an efficient two-pointer technique running in linear time:

(i) Algorithm for elements in $\text{List } 1$ not in $\text{List } 2$ ($\text{List } 1 \setminus \text{List } 2$):
Algorithm Difference_L1_L2(L1, L2): 1. Let i = 1 (pointer for L1), j = 1 (pointer for L2) 2. Let Result = empty list 3. While i ≤ length(L1) and j ≤ length(L2): If L1[i] < L2[j]: Add L1[i] to Result i = i + 1 Else if L1[i] == L2[j]: i = i + 1 j = j + 1 Else: (L1[i] > L2[j]) j = j + 1 4. While i ≤ length(L1): Add L1[i] to Result i = i + 1 5. Report Result

(ii) Algorithm for elements in $\text{List } 2$ not in $\text{List } 1$ ($\text{List } 2 \setminus \text{List } 1$):
Algorithm Difference_L2_L1(L1, L2): 1. Let i = 1 (pointer for L1), j = 1 (pointer for L2) 2. Let Result = empty list 3. While i ≤ length(L1) and j ≤ length(L2): If L2[j] < L1[i]: Add L2[j] to Result j = j + 1 Else if L2[j] == L1[i]: i = i + 1 j = j + 1 Else: (L2[j] > L1[i]) i = i + 1 4. While j ≤ length(L2): Add L2[j] to Result j = j + 1 5. Report Result
The two-pointer approach scans both lists simultaneously in $O(\text{len}_1 + \text{len}_2)$ steps, outputting elements found strictly in one list.
Exercise 11.2 • Question 2
NCERT
Describe an algorithm to compute the least common multiple ($\text{lcm}$) of two numbers.
Step-by-Step Solution:
We can design two standard algorithmic approaches:

Method 1: Direct Multiples Generation Method:
Algorithm LCM_Direct(m, n): 1. Let larger = max(m, n) 2. Let smaller = min(m, n) 3. Let multiple = larger 4. While (multiple mod smaller) ≠ 0: multiple = multiple + larger 5. Report multiple as lcm(m, n)

Method 2: Using the Fundamental Theorem Relation $\text{lcm}(m, n) = \frac{m \times n}{\gcd(m, n)}$:
Algorithm LCM_Via_GCD(m, n): 1. Compute g = gcd(m, n) using Euclid's / Āryabhaṭa's algorithm 2. Compute result = (m × n) / g 3. Report result as lcm(m, n)
This second algorithm is extremely fast because Euclid's algorithm computes $\gcd(m, n)$ in logarithmic steps!
Optimal Algorithm: Compute $\gcd(m, n)$ using Euclid's division, then return $\mathbf{\frac{m \times n}{\gcd(m, n)}}$.
Exercise 11.2 • Question 3
Star ProblemNCERT
Divisors occur in pairs. For instance, the divisors of $18$ are $(1, 18)$, $(2, 9)$ and $(3, 6)$.
(i) If we write out divisors in pairs, how many numbers do we have to examine between $1$ and $n$ to find all the divisors of $n$?
(ii) If we list out the divisors in pairs, will our $\gcd$ algorithm still work in the manner we have described?
Step-by-Step Solution:
(i) Number of values to examine:
  • If $d$ divides $n$, then $n/d$ also divides $n$, and $d \times (n/d) = n$.
  • If both factors were strictly greater than $\sqrt{n}$, their product would exceed $\sqrt{n} \times \sqrt{n} = n$, a contradiction.
  • Therefore, in every divisor pair, at least one factor must be $\le \sqrt{n}$.
  • Consequently, we only need to test numbers from $1$ up to $\mathbf{\lfloor\sqrt{n}\rfloor}$. For example, for $n = 100$, we only examine numbers $1$ to $10$, rather than all $100$ numbers!

(ii) Will our $\gcd$ algorithm still work?
  • Yes, but with one crucial operational requirement: when divisors are found in pairs $(d, n/d)$, they are not naturally generated in ascending sorted order (e.g. for $18$: $1, 18, 2, 9, 3, 6$).
  • To find the $\gcd$ by taking the rightmost common element (or largest common divisor), we can either:
    1. Insert smaller divisors at the front and complementary larger divisors at the back to maintain sorted order, OR
    2. Simply collect all divisors into a set and find the maximum common element.
(i) We only need to examine up to $\lfloor\sqrt{n}\rfloor$ numbers. (ii) Yes, provided we sort or properly assemble the pairs into an ordered list.

Exercise Set 11.3

2 Questions
Exercise 11.3 • Question 1
NCERT
How would our original algorithm change if we computed the divisors of $n$ by examining the numbers from $1$ to $n$ in reverse order, from $n$ down to $1$?
Step-by-Step Solution:
In the original algorithm, divisors were tested starting from $1$ up to $n$, which yielded the list in increasing order, making the rightmost element the largest.

If we examine numbers in reverse order from $n$ down to $1$:
  • The list of divisors will now be generated in strictly decreasing order: $$\text{divisors}(18) = [18, 9, 6, 3, 2, 1]$$
  • Similarly, the list of common divisors will be sorted from largest to smallest.
  • Therefore, instead of reporting the rightmost element of common divisors, the greatest common divisor ($\gcd$) will now be the very first (leftmost) element of the list!
The lists are generated in decreasing order, so $\gcd$ is reported as the leftmost (first) element instead of the rightmost.
Exercise 11.3 • Question 2
NCERT
What about the last algorithm described above? What happens when we look at common divisors starting from $\min(m, n)$ and work backwards to $1$?
Step-by-Step Solution:
Consider an algorithm that tests potential common divisors starting at $k = \min(m, n)$ and decreases $k$ by $1$ in each step:
Algorithm Backward_GCD(m, n): 1. Let k = min(m, n) 2. While k ≥ 1: If (m mod k == 0) and (n mod k == 0): Return k # STOP IMMEDIATELY! k = k - 1
Major Advantage (Early Termination):
  • Because we start from the largest possible candidate $\min(m, n)$ and move downwards, the very first number that divides both $m$ and $n$ is guaranteed to be the Greatest Common Divisor!
  • We can stop immediately without having to check any smaller numbers down to $1$.
  • For instance, if $m = 60$ and $n = 30$, $\min(60, 30) = 30$. It checks $30$: $60 \bmod 30 = 0$ and $30 \bmod 30 = 0$. It stops after just 1 single step, finding $\gcd = 30$ instantly!
The algorithm can halt on the very first match, saving computation by avoiding having to find or store all smaller common divisors.

End-of-Chapter Exercises

5 Questions
End-of-Chapter • Question 1
NCERT
Compute the following using the improved version of Euclid's algorithm (Āryabhaṭa's division method):
(i) $\gcd(375, 825)$
(ii) $\gcd(51000, 81000)$
(iii) $\gcd(1789287, 237656)$
(iv) $\gcd(2587392, 157656)$
Step-by-Step Solution:
Euclid's division lemma states: $a = bq + r$, where $0 \le r < b$. Then $\gcd(a, b) = \gcd(b, r)$.

(i) $\gcd(375, 825)$:
  • Step 1: Divide $825$ by $375$: $$825 = 375 \times 2 + 75$$
  • Step 2: Divide $375$ by $75$: $$375 = 75 \times 5 + 0$$
The last non-zero remainder is $75$. Thus, $\mathbf{\gcd(375, 825) = 75}$.

(ii) $\gcd(51000, 81000)$: Notice that $1000$ is a common factor: $\gcd(51000, 81000) = 1000 \times \gcd(51, 81)$. Let us execute the division steps:
  • Step 1: $81000 = 51000 \times 1 + 30000$
  • Step 2: $51000 = 30000 \times 1 + 21000$
  • Step 3: $30000 = 21000 \times 1 + 9000$
  • Step 4: $21000 = 9000 \times 2 + 3000$
  • Step 5: $9000 = 3000 \times 3 + 0$
The last non-zero remainder is $3000$. Thus, $\mathbf{\gcd(51000, 81000) = 3000}$.

(iii) $\gcd(1789287, 237656)$:
  • Step 1: $1789287 = 237656 \times 7 + 125695$
  • Step 2: $237656 = 125695 \times 1 + 111961$
  • Step 3: $125695 = 111961 \times 1 + 13734$
  • Step 4: $111961 = 13734 \times 8 + 2089$
  • Step 5: $13734 = 2089 \times 6 + 1200$
  • Step 6: $2089 = 1200 \times 1 + 889$
  • Step 7: $1200 = 889 \times 1 + 311$
  • Step 8: $889 = 311 \times 2 + 267$
  • Step 9: $311 = 267 \times 1 + 44$
  • Step 10: $267 = 44 \times 6 + 3$
  • Step 11: $44 = 3 \times 14 + 2$
  • Step 12: $3 = 2 \times 1 + 1$
  • Step 13: $2 = 1 \times 2 + 0$
The last non-zero remainder is $1$. Thus, $\mathbf{\gcd(1789287, 237656) = 1}$ (the numbers are coprime).

(iv) $\gcd(2587392, 157656)$:
  • Step 1: $2587392 = 157656 \times 16 + 64896$
  • Step 2: $157656 = 64896 \times 2 + 27864$
  • Step 3: $64896 = 27864 \times 2 + 9168$
  • Step 4: $27864 = 9168 \times 3 + 360$
  • Step 5: $9168 = 360 \times 25 + 168$
  • Step 6: $360 = 168 \times 2 + 24$
  • Step 7: $168 = 24 \times 7 + 0$
The last non-zero remainder is $24$. Thus, $\mathbf{\gcd(2587392, 157656) = 24}$.
(i) $75$, (ii) $3000$, (iii) $1$, (iv) $24$.
End-of-Chapter • Question 2
NCERT
Assume that $m \ge n$. Verify that $d$ divides $m$ and $n$ if and only if $d$ divides both $n$ and $m \bmod n$.
Step-by-Step Solution:
By Euclidean division, we can express $m$ in terms of $n$: $$m = q \cdot n + r$$ where $q = \lfloor m/n \rfloor$ is the quotient and $r = m \bmod n$ is the remainder. This equation can be rewritten as: $$r = m - q \cdot n$$
Direction 1 ($\implies$): Suppose $d$ divides both $m$ and $n$:
  • Since $d \mid m$, there exists an integer $k_1$ such that $m = k_1 d$.
  • Since $d \mid n$, there exists an integer $k_2$ such that $n = k_2 d$.
  • Substitute into the expression for $r$: $$r = m - q n = k_1 d - q (k_2 d) = (k_1 - q k_2) d$$
  • Since $(k_1 - q k_2)$ is an integer, $d$ divides $r = (m \bmod n)$.
  • Since $d \mid n$ by assumption, $d$ divides both $n$ and $m \bmod n$.

Direction 2 ($\impliedby$): Suppose $d$ divides both $n$ and $r = m \bmod n$:
  • Let $n = k_2 d$ and $r = k_3 d$ for integers $k_2, k_3$.
  • Substitute into the formula for $m$: $$m = q n + r = q(k_2 d) + k_3 d = (q k_2 + k_3) d$$
  • Since $(q k_2 + k_3)$ is an integer, $d$ divides $m$.
  • Hence $d$ divides both $m$ and $n$.
The set of common divisors of $\{m, n\}$ is identically equal to the set of common divisors of $\{n, m \bmod n\}$. Hence their greatest common divisors are equal: $\mathbf{\gcd(m, n) = \gcd(n, m \bmod n)}$.
End-of-Chapter • Question 3
NCERT
Write an algorithm $\text{prime}(n)$ to check if $n$ is prime.
(Hint: A prime number $p$ has exactly two distinct factors, $1$ and $p$. Can you make use of $\text{divisors}(n)$ to write out $\text{prime}(n)$?)
Step-by-Step Solution:
By definition, a natural number $n$ is prime if and only if $n > 1$ and its only divisors are $1$ and $n$.

Algorithm (using trial division up to $\sqrt{n}$):
Algorithm Prime(n): 1. If n ≤ 1: Return False # 1 and negative numbers are not prime 2. If n == 2 or n == 3: Return True 3. If n mod 2 == 0 or n mod 3 == 0: Return False # Quick filter for even numbers and multiples of 3 4. Let d = 5 5. While d × d ≤ n: If (n mod d == 0) or (n mod (d + 2) == 0): Return False d = d + 6 6. Return True # No factor found up to √n → n is prime!
Alternative using divisors list:
Algorithm Prime_Via_Divisors(n): 1. Let D = divisors(n) 2. If length(D) == 2: Return True 3. Else: Return False
The algorithm checks whether $n$ has any divisor strictly between $1$ and $n$ (checking up to $\lfloor\sqrt{n}\rfloor$), returning True if prime and False otherwise.
End-of-Chapter • Question 4
Star ProblemNCERT
Write an algorithm $\text{primedivisors}(n)$ to compute the list of divisors of $n$ that are prime numbers.
(Hint: Compute $\text{divisors}(n)$ and then filter out the primes in this list.)
Step-by-Step Solution:
We combine the divisor-finding algorithm with the primality test algorithm to filter out only prime divisors:
Algorithm PrimeDivisors(n): 1. Let all_divisors = Divisors(n) # Get list of all factors of n 2. Let prime_divs = empty list 3. For each d in all_divisors: If Prime(d) is True: Add d to prime_divs 4. Return prime_divs
Example trace for $n = 60$:
  • $\text{Divisors}(60) = [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]$
  • Filter primes:
    • $1$ is not prime (skip)
    • $2$ is prime $\implies$ keep
    • $3$ is prime $\implies$ keep
    • $4, 6$ are composite (skip)
    • $5$ is prime $\implies$ keep
    • $10, 12, 15, 20, 30, 60$ are composite (skip)
  • Output: $[2, 3, 5]$
$\text{PrimeDivisors}(n)$ generates all divisors and returns a sub-list containing only those elements where $\text{Prime}(d) = \text{True}$.
End-of-Chapter • Question 5
Star ProblemNCERT
We can also find the $\gcd$ of two numbers by computing prime factorisation of both the numbers. Try to write an algorithm to compute the prime factorisation of a number.
(i) The prime factorisation of $180$ is $2^2 \times 3^2 \times 5^1$. How would you represent this?
(ii) How would you compare the prime factorisations of two numbers?
Step-by-Step Solution:
Algorithm for Prime Factorisation:
Algorithm PrimeFactorisation(n): 1. Let factors = empty list of pairs (prime, power) 2. Let d = 2 3. While d × d ≤ n: If n mod d == 0: count = 0 While n mod d == 0: count = count + 1 n = n / d Add (d, count) to factors d = d + 1 4. If n > 1: Add (n, 1) to factors 5. Return factors

(i) Representation of $180 = 2^2 \times 3^2 \times 5^1$: In an algorithm, we can represent this as:
  • A list of pairs: $[(2, 2), (3, 2), (5, 1)]$ where each element is $(\text{prime}, \text{exponent})$.
  • Or a dictionary/map: $\{2: 2, 3: 2, 5: 1\}$.

(ii) Comparing Prime Factorisations to compute GCD and LCM: Let $A = p_1^{a_1} p_2^{a_2} \dots p_k^{a_k}$ and $B = p_1^{b_1} p_2^{b_2} \dots p_k^{b_k}$ over all primes involved (with exponents $0$ if a prime does not divide the number):
  • Greatest Common Divisor ($\gcd$): Take the minimum exponent of each prime factor common to both: $$\gcd(A, B) = p_1^{\min(a_1, b_1)} \cdot p_2^{\min(a_2, b_2)} \cdots p_k^{\min(a_k, b_k)}$$
  • Least Common Multiple ($\text{lcm}$): Take the maximum exponent of each prime factor: $$\text{lcm}(A, B) = p_1^{\max(a_1, b_1)} \cdot p_2^{\max(a_2, b_2)} \cdots p_k^{\max(a_k, b_k)}$$
Example: $180 = 2^2 \times 3^2 \times 5^1$ and $120 = 2^3 \times 3^1 \times 5^1$. $$\gcd(180, 120) = 2^{\min(2, 3)} \times 3^{\min(2, 1)} \times 5^{\min(1, 1)} = 2^2 \times 3^1 \times 5^1 = 4 \times 3 \times 5 = \mathbf{60}$$
Represent factorisation as list of pairs $(\text{prime}, \text{exponent})$. Compare by taking $\min(\text{exponents})$ for GCD and $\max(\text{exponents})$ for LCM.