• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Introduction To Number Theory ( Part 1 )

December 27, 2017 by Dhaval Dave

Number Theory is used a lot for solving real world problems and for the same reason It’s asked most of the time in interviews. This article marks the beginning of series of articles over Number Theory. Today we will discuss following algorithms –

  • Euclid’s Algorithm For Finding GCD
  • Extended Euclid’s Algorithm
  • Modular Exponenetiation
  • Modular Multiplicative Inverse

Euclid’s Algorithm For Finding GCD


Finding GCD( Greatest Common Divisor ) of two numbers is a very common problem which occurs as a sub problem in many large problems. The naive approach to find GCD is to iterate over all integers till the smaller of two numbers and then find the largest number which divides both numbers. The complexity of naive approach is O(min(a,b)) [a,b are numbers whose GCD is computed]. The Euclid’s algorithm reduces this complexity to O(log(max(a,b)). In this algorithm we exploit the following property –

gcd(a,b) = gcd(b,a%b)

With the above recurrence relation we can easily write recursive code to compute GCD with the base case of b = 0. In iterative implementation we will iterate until b > 0. In each iteration we will update the value of a with value of previous b and value b with modular value of previous a with previous b.

Code

The code below provides the iterative implementation of Euclid’s Algorithm of finding GCD.


int gcd(int a, int b){

    int A = max(a,b), B = min(a,b), temp;
    
    while(B > 0){
        
        temp = A % B;
        A = B;
        B = temp;

    }

    return A;

}

The complexity of computing GCD using above algorithm is O(log(max(a,b)).

Extended Euclid’s Algorithm

Extended Euclidean theorem states that, Given two numbers a and b then their GCD can be expressed as their linear combination ie.

ax + by = gcd(a,b)  x,y \in Z

These coefficients x and y are used a lot while computing modular multiplicative inverse. We compute these coefficients using Extended Euclid’s algorithm. Before delving right into the algorithm let’s solve few equations first.


Let, 1. ax1 + by1 = gcd(a,b)
     2. bx2 + (a % b)y2 = gcd(b,a % b)

Also (a % b) = a - \lfloora/b\rfloor where \lfloor\rfloor represents greatest integer function.

Substituting this value in equation 2 we get,

bx2 + (a - \lfloora/b\rfloor)y2 = gcd(b,a % b)

Rearranging above equation we get,

ay2 + b(x2 - \lfloora/b\rfloory2) = gcd(b,a % b)

Now as discussed previously,

gcd(b,a % b) = gcd(a,b)

Finally we get,

ay2 + b(x2 - \lfloora/b\rfloory2) = gcd(a,b)

Comparing the coefficients of the above equation with equation 1 we get,

x1 = y2
y1 = x2 - \lfloora/b\rfloory2

The last two equations gives us the insight that the we can compute these coefficients in recursive manner. The code for recursive implementation will be almost similar to that of recursive code for simple Euclidean algorithm. All we have to do additionally is to update the values of these coefficients at the end of each recursive call according to equations derived above.

Code

Code for computing the coefficients is almost similar to code for recursive computation of GCD.

#include <bits/stdc++.h>
using namespace std;

int x,y;

void extendedEuclid(int a, int b){

	// Base case ...........
	if(b == 0){

		x = 1;
		y = 0;
		return;

	}

	extendedEuclid(b,a % b);

        // Updating coefficients with new values ................
	int temp = x;
	x = y;
	y = temp - (a/b)*y;

}

// Driver function ..........
int main(){

	int a, b;
	cin >> a >> b;

	extendedEuclid(a,b);
	cout << "x = " << x << " y = " << y << endl;

}

Complexity of computing coefficients using above algorithm is same as that of Euclidean algorithm for computing GCD ie. O(log(max(a,b))).

Modular Exponentiation

Most of the times we are required to compute ab modulo some value m, Where b can be as large as 1018. The naive approach to solve this problem is to iterate till b and in each iteration multiply ‘a’ to result under modular arithmetic. The complexity of naive approach is O(b) and with b being as large as 1018 this approach becomes computationally infeasible.

The technique which is used to solve this problem is Binary Exponentiation. Binary Exponentiation exploits the following property –

ab = (a2)b/2 if b % 2 == 0 (ie. b is even)
ab = a * (a2)(b - 1)/2 if b % 2 == 1 (ie. b is odd)

The above two properties also serve the purpose of recurrence relation needed to write recursive code for this problem. In the recursive implementation we will check if b is odd or even. If it is even then we will solve the sub problem with new a = a * a and new b = b/2. If it is odd we will first solve the new sub problem with a = a * a and b = (b – 1)/2 and will then multiply the answer thus obtained with a. The base case will be when b = 0 in that case we will simply return value 1.

Code

The code below provides the code for both recursive and iterative implementation of above problem.

#include <bits/stdc++.h>
using namespace std;

long long int recursiveModularPow(long long int a, long long int b, long long int mod){

	// base case .........
	if(b == 0)
		return 1;

	// b is even .........
	else if(b % 2 == 0) 
		return recursiveModularPow((a * a) % mod, b/2, mod);

	// b is odd .........
	else
		return (a * recursiveModularPow((a * a) % mod, (b -1)/2 , mod)) % mod;

}

long long int iterativeModularPow(long long int a,long long int b, long long int mod){

	long long int result = 1;

	while(b > 0){

		if(b % 2 == 1){

			result *= a;
			result %= mod;

		}

		a *= a;
		a %= mod;
		b /= 2; 

	}

	return result;

}

int main(){

	long long int a, b, m;
	cin >> a >> b >> m;

	cout << iterativeModularPow(a, b, m) << endl;

}

In both recursive and iterative implementation at each iteration the value of b gets halved and the computation ends when b becomes zero. So the overall complexity of both these codes is O(log2b).

Modular Multiplicative Inverse

Multiplicative inverse of a number x under modulo m is a number y \in [1, m -1] st. –

(x * y)%m = 1   ie. x-1 mod m = y

Now the multiplicative inverse of a number x under modulo m exists iff gcd(x,m) = 1. Also according to Extended Euclid’s algorithm we can represent gcd(x,m) as linear combination of x and m ie.

ax + bm = 1     a, b \in Z

Taking modulo with m on both sides of above equation we get,

(ax)%m + (bm)%m = 1%m

Now as bm is multiple of hence (bm)%m = 0. Also 1%m = 1. So the above equation reduces to –

(ax)%m = 1

From the above equation we can see that the coefficient ‘a’ is the required multiplicative inverse. This coefficient can be easily computed using Extended Euclid’s algorithm that was discusses above. An another technique exists when the value under which we want to find inverse is prime but it fails for other cases. We will discuss that technique in next article till then stay tuned.

This Article is Published by Abhey Rana.
If you want to be content writer with Gohired.in Please write at career@gohired.in or admin@gohired.in

Similar Articles

Filed Under: Algorithm, Amazon Interview Question, Flipkart Interview Questions, Interview Questions, Microsoft Interview Questions Tagged With: Euclid's Algorithm, Modular Exponentiation, Number Theory

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Implement LRU Cache

Count number of ways to reach a given score in a game

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Mirror of Tree

VMWare SDEII Interview

Closed Parentheses checker

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Find the smallest window in a string containing all characters of another string

Flipkart Set 1 On Campus with Answers

Code Chef PRGIFT Solution

Difference between a LinkedList and a Binary Search Tree BST

Edit Distance ( Dynamic Programming )

Urban Ladder Written Test.

Find two non repeating elements in an array of repeating elements

Maximum difference between two elements s.t larger element appears after the smaller number

1014 Practice Question of New GRE – Princeton

FizzBuzz Solution C C++

Printing intermediate Integers between one element & next element of array

Find Pythagorean Triplets in an array in O(N)

Leetcode: Edit Distance

Find Percentage of Words matching in Two Strings

SAP Off Campus Hiring_ March 2015 Verbal Skills

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Stock Buy Sell to Maximize Profit

Generate largest number arranging a no. of given non negative integer numbers

Calculate price of parking from parking start end time prices

Get K Max and Delete K Max in stream of incoming integers

K’th Largest Element in BST when modification to BST is not allowed

Number of Islands BFS/DFS

Copyright © 2026 · Genesis Framework · WordPress · Log in