Euclid’s Algorithm

Reference book: Data Structures and Algorithm Analysis in C++, writer is Mark Allen Weiss.
Performance: 1.44logN

Python:

def gcd(m,n):
    while(n != 0):
        rem = m % n
        m = n
        n = rem
    return m

print gcd(1989,1590)

C++:

#include <iostream>
using namespace std;

long gcd(long m, long n)
{
    while(n != 0)
    {
        long rem = m % n;
        m = n;
        n = rem;
    }

    return m;
}

int main()
{
    cout << gcd(1989, 1590) << endl;
}

This entry was posted in Algorithm. Bookmark the permalink.

Leave a comment