Determine the greatest common divisor, Data Structure & Algorithms

Determine the greatest common divisor (GCD) of two integers, m & n. The algorithm for GCD might be defined as follows:

While m is greater than zero:

If n is greater than m, swap m and n.

Subtract n from m.

n is the GCD

Code in C

int gcd(int m, int n)

/* The precondition are following: m>0 & n>0. Let g = gcd(m,n). */

{

while( m > 0 )

{

if( n > m )

{ int t = m; m = n; n = t; } /* swap m & n*/

/* m >= n > 0 */

m - = n;

}

return n;

}

Posted Date: 4/4/2013 6:17:40 AM | Location : United States







Related Discussions:- Determine the greatest common divisor, Assignment Help, Ask Question on Determine the greatest common divisor, Get Answer, Expert's Help, Determine the greatest common divisor Discussions

Write discussion on Determine the greatest common divisor
Your posts are moderated
Related Questions
Write c++ function to traverse the threaded binary tree in inorder traversal

Speed cameras read the time a vehicle passes a point (A) on road and then reads time it passes a second point (B) on the same road (points A and B are 100 metres apart). Speed of t

Q. Perform implementation of a queue using a singly linked list L. The operations INSER and DELETE should take O (1) time.

/* the program accepts two polynomials as a input & prints the resultant polynomial because of the addition of input polynomials*/ #include void main() { int poly1[6][

Explain Floyd's algorithm It is convenient to record the lengths of shortest paths in an n by n matrix D known as the  distance matrix: the element d ij   in the i th   row an

difference between recursion and iteration

Define Binary Tree  A binary tree T is explained as a finite set of nodes that is either empty or having of root and two disjoint binary trees TL, and TR known as, respectively

write a program that find,search&replace a text string

Advantages of First in First out (FIFO) Costing Advantages claimed for first in first  out (FIFO)  costing method are: 1. Materials used are drawn from the cost record in

Algorithm to find sum of square of a number