/ Published in: ActionScript 3
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; }