标签:style blog io ar 使用 java sp on 2014
比欧几里得算法高效的用来计算gcd和lcm的stein算法,用来大数的计算:function gcd(a,b){ if(a == b){return a;} var bigger; var smaller; if(a>b){bigger = a;smaller = b;} else{bigger = b;smaller = a;} if(smaller == 0){return bigger;} if(bigger %2 == 0 && smaller % 2 == 0){ return 2*gcd(bigger/2,smaller/2); } else if(bigger%2 == 0){ return gcd(bigger/2,smaller); } else if(smaller%2 == 0){ return gcd(bigger,smaller/2); } else{ return gcd((bigger+smaller)/2,(bigger-smaller)/2); } } function lcm(a,b){ return a*b/gcd(a,b); }
标签:style blog io ar 使用 java sp on 2014
原文地址:http://blog.csdn.net/lan_liang/article/details/40870609