# Posted By

b121417 on 04/24/10

# Statistics

Viewed 283 times
Favorited by 0 user(s)

# Greatest Common Factor

/ Published in: ActionScript 3

Copy this code and paste it in your HTML
1. public function getGreatestCommonFactor( e1:uint, e2:uint, e3:uint ):uint{
2. var elements = new Array( e1, e2, e3 );
3. var gcd:uint;
4.
5. var factors:Array = new Array();
6. var cnt:uint=0;
7. var qCnt:uint=0;
8. var elementCount:uint=elements.length;
9.
10. //find the min number
11. var minNum:uint = Math.min ( elements[0], elements[1], elements[2] );
12. trace("Minnum" + minNum);
13.
14. //loop thru all values of min from 1
15. for(var i=1;i<=minNum;i++){
16.
17. //test for remainder for all 3
18. for(var k=0;k<elements.length;k++){
19. if(elements[k]%i==0){
20. cnt++;
21. }
22. }
23. //if can be div by all 3
24. if(cnt==elements.length){
25. trace("Factor found");
26. factors[qCnt]=i;
27. qCnt++;
28. }
29. cnt=0;
30. }
31.
32. //Find the greatest factor
33. for( i=0;i<qCnt;i++){
34. if(gcd<factors[i]){
35. gcd=factors[i];
36. }
37. }
38.
39. trace( "Factors: " + factors );
40. return gcd;
41. }