Greatest Common Divisor : Java


/** 
* Gets GCD between two positive integers.   
* source: http://www.victorbyte.net  
* Returns int value   
*/ 
public static int getGCD(int numA, int numB)
{
    if(numA < numB)
    {
        int temp = numA;
        numA = numB;
        numB = temp;
    }

    if( numB <= 0 ) return 0;

    if((numA % numB) == 0)
        return numB;

    return getGCD( numB , numA %= numB );
}
This entry was posted in Code samples and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *