logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
ArraysBoyer-Moore Majority Vote AlgorithmKadane’s AlgorithmTriple Reverse algorithm aka Reversal Algorithm
Cycle detectionDijkstraDisjoint Set UnionGraphTopological SortTraversal algorithmsTree Traversal
Binary HeapBinomial HeapHeap
Cycle DetectionLinked ListSorting
Finding the majority elementRotate array by K steps
Finding If Number is PrimeGCD of two numbersPerforming ExponentiationPrime NumbersRandomising Data
3. Max Consecutive OnesHoare'S Partition SchemeLomuto Partition Scheme
DequesPriority QueueQueue
The Binary Search Master GuideThe Rotated Sorted Array Master Guide
Bubble SortCounting SortInsertion SortMerge SortPigeon Hole SortQuick SortSelection Sort
Monotonic StackMonotonic StackStackReversing an ArrayStackTower of Hanoi
Binary Tree TraversalCentre of gravityDiameter of TreeShortest Path
Algorithmic Pattern/numerical algorithms

GCD of two numbers

2 min read
On this pageCalculating Greatest Common Divisors <a href="#head-3-8" id="head-3-8"></a>
Calculating Greatest Common Divisors <a href="#head-3-8" id="head-3-8"></a>Steps of the AlgorithmComplexity Analysis

The Greatest Common Divisor (GCD), or Highest Common Factor (HCF), is the largest number that divides two numbers without a remainder.

# Calculating Greatest Common Divisors

A naive approach to this is to iterate from all numbers between 1 to min(m,n)\text{min}(m,n)min(m,n) and return the biggest number that divides them

int gcd(int m, int n) {
	int maxDivisor = 1;
	for (int i = 1; i &lt; min(m,n); ++i) {
		if(m%i == 0 && n%i ==0) {
			maxDivisor = i;	
		}
	}
	return i;
}

Info

The GCD is always positive irrespective of the sign of the input numbers. If the given number is negative, we will simply ignore its - sign using the abs() function

To find the GCD of two numbers using recursion, we will be using the principle GCD(a,b)=GCD(a,b−a)\text{GCD}(a,b) = \text{GCD}(a,b−a)GCD(a,b)=GCD(a,b−a)

<figure>

# Steps of the Algorithm

  1. Check if a==0, if yes, return b.
  2. Check if b==0, if yes, return a.
  3. Check if a==b, if yes, return a.
  4. Check if a>b if yes, call GCD() function using the arguments a−b and b recursively, otherwise call GCD() function using the arguments a and

# Implementation

int gcd(int a , int b) {
  if(a == 0) return b;
  if(b == 0) return a;
  return  a > b ? gcd(a-b,b) : gcd(a,b-a);
}

# Complexity Analysis

# Time Complexity

O(max(a,b))\text{O}(\text{max}(a,b))O(max(a,b)) In this algorithm, the number of steps are linear, for e.g. GCD(x,1)GCD(x,1)GCD(x,1) in which we will subtract 111 from xxx in each recursion, so the time complexity will be O(max(a,b))\text{O}(\text{max}(a,b))O(max(a,b))

# Space Complexity

The space complexity here is O(max(a,b))\text{O}(\text{max}(a,b))O(max(a,b)) because the space complexity in a recursive function is equal to the maximum depth of the call stack.


←PreviousFinding If Number is PrimeNext→Performing Exponentiation

On this page

Calculating Greatest Common Divisors <a href="#head-3-8" id="head-3-8"></a>Steps of the AlgorithmComplexity Analysis