Calculate greatest common divisor and least common multiple using the Stein algorithm

Source: Internet
Author: User
Tags gcd greatest common divisor
The Stein algorithm, which is efficient for calculating gcd and LCM, is used to calculate large numbers:


function gcd (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);}


Calculate greatest common divisor and least common multiple using the Stein algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.