{"id":445,"date":"2009-11-28T08:04:31","date_gmt":"2009-11-28T15:04:31","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2477"},"modified":"2009-11-28T08:04:31","modified_gmt":"2009-11-28T15:04:31","slug":"greatest-common-factor-divisor","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=445","title":{"rendered":"Greatest Common Factor (divisor)"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\ntrace(gcf(75,145));\r\n\r\n\/\/ outputs 5\r\n\r\nfunction gcf(a:int, b:int):int{\r\n\tvar remainder:int;\r\n\tvar factor:Number = 0;\r\n\twhile (1){\r\n\t\tif (b &gt; a){\r\n\t\t   var swap:int = a;\r\n\t\t   a = b;\r\n\t\t   b = swap;\r\n\t\t}\r\n\t\tremainder = a % b;\r\n\t\ta = b;\r\n\t\tb = remainder\r\n\t\tif (remainder == 0){\r\n\t\t\tfactor = a;\r\n\t\t\tbreak;\r\n\t\t}else if (remainder==1){\r\n          \r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n\treturn factor;\r\n}\r\n<\/pre>\n<p>I was messing around with <a href=\"http:\/\/en.wikipedia.org\/wiki\/Egyptian_fraction\" target=\"blank\">Egyptian Fractions<\/a> and found myself in need of some interesting functions. The first function I realized I would be needing was for a greatest common factor (GCF or GCD). <\/p>\n<p>The above snippet is a quick implementation of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Greatest_common_divisor#Using_Euclid.27s_algorithm\" target=\"blank\">Euclid&#8217;s<\/a> algorithm. It will return 0 if no GCF is found&#8230;<\/p>\n<p>I wrote a few helper functions while working with Egyptian fractions&#8230; will post them over the next few days.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>trace(gcf(75,145)); \/\/ outputs 5 function gcf(a:int, b:int):int{ var remainder:int; var factor:Number = 0; while (1){ if (b &gt; a){ var swap:int = a; a = b; b = swap; } remainder = a % b; a = b; b = remainder if (remainder == 0){ factor = a; break; }else if (remainder==1){ break; } } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[3,14,4],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category-math","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=445"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/445\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}