Introduction – What is an Algorithm?
Basic Definition:
An algorithm is a step-by-step method used to solve a problem in a logical and organized way.
Set of rules – A group of rules that help complete a calculation, either by hand or using a machine.
Well-defined computational procedure – A method that takes input, processes it, and gives an output.
Finite sequence of steps – A process with a fixed number of steps to reach a result.
Where does the word "Algorithm" come from?
The word algorithm comes from the name of a 9th-century mathematician,
Abdullah Jafar Muhammad ibn Musa Al-Khwarizmi.
What an Algorithm is NOT:
- An algorithm is not a programming language.
- It is not actual code, but a step before coding.
- It is usually written in simple language, so anyone can understand it.
Useful Qualities of a Great Algorithm:
General – It should work for more than one specific problem.
Efficient – Uses less time and less memory.
Understandable – Easy for anyone to read and follow.
Unambiguous – Each step should be clear and not confusing.
What is GCD (Greatest Common Divisor)?
The GCD of two numbers is the largest number that completely divides both numbers.
Example:
GCD of 8 and 12 is 4,
because 4 divides both 8 and 12, and no bigger number does.
What is Euclid’s Algorithm?
Euclid’s Algorithm is a very old and efficient method to find the GCD of two numbers.
It was invented by the Greek mathematician Euclid.
Steps of Euclid’s Algorithm (in Simple Words):
Input: Two positive integers a and b where a > b
-
If b = 0, then a is the GCD → Stop
-
Otherwise:
-
Find the remainder
r = a mod b
-
Replace:
a = b
b = r
-
Go back to Step 1
-
Repeat this process until the remainder becomes 0.
Let’s Understand with an Example:
Find GCD of 1071 and 462
Iteration | a | b | r = a mod b | Next Step (a ← b, b ← r) | |
---|---|---|---|---|---|
1 | 1071 | 462 | 147 | a = 462, b = 147 | |
2 | 462 | 147 | 21 |
a = 147, b = 21 | |
3 | 147 | 21 | 0 | Done! |
When b = 0
, we stop.
Final Answer: GCD = 21
Pseudo-code of Euclid’s Algorithm:
Algorithm GCD-Euclid(a, b)
begin
while b ≠ 0 do
r ← a mod b
a ← b
b ← r
return a
end
Key Points:
-
Inputs: Two positive integers a and b
-
Output: GCD of a and b
-
Terminates: Always finishes in finite steps
-
Efficient: Very fast, even for large numbers
Real-Life Uses of GCD:
-
To simplify fractions
(e.g., 18/24 becomes 3/4 after dividing both by GCD = 6) -
In Cryptography, especially RSA algorithm
-
In Number Theory and Math competitions