Return to Snippet

Revision: 26352
at April 24, 2010 12:22 by b121417


Initial Code
public function getGreatestCommonFactor( e1:uint, e2:uint, e3:uint ):uint{
	var elements = new Array( e1, e2, e3 );
	var gcd:uint;
			
	var factors:Array = new Array();
	var cnt:uint=0;
	var qCnt:uint=0;
	var elementCount:uint=elements.length;
			
	//find the min number
	var minNum:uint = Math.min ( elements[0], elements[1], elements[2] );
	trace("Minnum" + minNum);
			
	//loop thru all values of min from 1
	for(var i=1;i<=minNum;i++){
				
	//test for remainder for all 3
	for(var k=0;k<elements.length;k++){
		if(elements[k]%i==0){
			cnt++;
		}
	}
	//if can be div by all 3
	if(cnt==elements.length){
		trace("Factor found");
		factors[qCnt]=i;
		qCnt++;
	}
		cnt=0;
}
			
	//Find the greatest factor
	for( i=0;i<qCnt;i++){
		if(gcd<factors[i]){
			gcd=factors[i];
		}
	}
			
	trace( "Factors: " + factors );
	return gcd;
}

Initial URL

                                

Initial Description

                                

Initial Title
Greatest Common Factor

Initial Tags
math

Initial Language
ActionScript 3