What is an Algorithm?

An algorithm is a finite sequence of well-defined steps used to solve a problem or perform a particular task. In mathematics, algorithms help us perform calculations systematically and obtain accurate results.
 
Characteristics of an algorithm:
1. Input: It accepts the required data.
 
2. Definiteness: Each step must be clear and unambiguous.
 
3. Finiteness: It must finish after a finite number of steps.
 
4. Output: It produces a result.
 
5. Effectiveness: Each step must be simple and executable. 
Divisors of a Number
A divisor of a number is a positive integer that divides the given number exactly, leaving no remainder. Divisors are also called factors of a number.
Example:
Consider the number \(18\).
 
The positive divisors of \(18\)  are: \( 1,\ 2,\ 3,\ 6,\ 9,\ 18 \)
Algorithm to find the divisors of n

1. Start with an empty list-of-divisors.
 
2. For each number j in the sequence 1,2,3,...,n
 
– if j divides n, add j to the list-of-divisors.
Example:
Let us see how this algorithm works for a small number, say \(18\).
 
• Initially list-of-divisors is empty [ ].
• \(1\) divides \(18\), so we update list-of-divisors to \([1]\).
• \(2\) divides \(18\), so we update list-of-divisors to \([1, 2]\).
• \(3\) divides \(18\), so we update list-of-divisors to \([1, 2, 3]\).
• \(4\) does not divide \(18\), so we leave list-of-divisors unchanged as \([1, 2, 3]\).
• \(5\) does not divide \(18\), so we leave list-of-divisors unchanged as \([1, 2, 3]\).
• \(6\) divides \(18\), so we update list-of-divisors to \([1, 2, 3, 6]\).
• \(7\) and \(8\) do not divide \(18\), so we leave list-of-divisors unchanged as \([1, 2, 3, 6]\).
• \(9\) divides \(18\), so we update list-of-divisors to \([1, 2, 3, 6, 9]\).
• \(10, 11, 12, … , 17\) do not divide \(18\), so we keep list-of-divisors unchanged as \([1, 2, 3, 6, 9]\).
• \(18\) divides \(18\), so we update list-of-divisors to \([1, 2, 3, 6, 9, 18]\).
Thus, at the end of the algorithm we have divisors\( (18) \ =\ [1, 2, 3, 6, 9, 18]\). 
 
Greatest Common Divisor (GCD)
The Greatest Common Divisor (GCD) of two or more positive integers is the greatest positive integer that divides each of the given numbers exactly. It is also known as the Highest Common Factor (HCF).
Example:
Find the GCD of \(12\) and \(18\).
 
Step 1: List the divisors of \(12\). \(D(12)=[1,2,3,4,6,12] \)
 
Step 2: List the divisors of \(18\). \(D(18)=[1,2,3,6,9,18] \)
 
Step 3: Identify the common divisors. \([1,2,3,6] \)
 
Step 4: Select the greatest common divisor. \( GCD(12,18) = 6\).
Algorithm to find gcd(m, n)
1. Let divisors of \(m\) be the list obtained by computing divisors\((m)\)
2. Let divisors of \(n\) be the list obtained by computing divisors \((n)\)
3. Start with an empty list common-divisors
4. For each number \(x\) in the list of divisors of \(m\). If \(x\) also appears in the list of divisors of \(n\) add \(x\) to the list common-divisors
5. Report the rightmost element of the list commondivisors as \(gcd(m,n)\).
Data Structures
A data structure is a systematic way of organising and storing data so that it can be accessed, processed and modified efficiently.
In an algorithm, we often need to store intermediate results while performing calculations.
Example:
While finding the divisors of \(18\), we store the divisors in a list: \([1,2,3,6,9,18] \). This list is an example of a data structure.