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
Q. Explain various graph traversal schemes and write their advantages and disadvantages. A n s . Graph Traversal Scheme is explained below In many troubles we wish

/* 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][

Algo rithm to Insert a Node p at the End of a Linked List is explained below Step1:   [check for space] If new1= NULL output "OVERFLOW" And exit Step2:   [Allocate fr


what is tree

State about the Simple types - Built-In Types Values of the carrier set are atomic, that is, they can't be divided into parts. Common illustrations of simple types are inte

This question is based on the requirements of a system to record band bookings at gigs. (A 'gig' is an event at which one or more bands are booked to play). You do not need to know

Explain the Scan-Line Algorithm This image-space method for removing hidden surfaces is an extension of the scan-line algorithm for filling polygon interiors. Instead of fillin

Ruby implementation of the Symbol ADT Ruby implementation of the Symbol ADT, as mentioned, hinges on making Symbol class instances immutable that corresponds to the relative la

What is the difference between a grounded header link list and a circular header link list? A header linked list is a linked list which always having a special node, known as t