javascriptは最大公約数を求めます.

8806 ワード

 1 doctype html>
 2 <head>
 3     <meta charset = "utf-8" />
 4 head>
 5 
 6 <body>
 7     <script>
 8         /**
 9                         
10             @param m   
11             @param n   
12             @return n      
13         */
14         const euclid = function(m,n){
15             let r;
16             while((r = m % n) != 0){
17                 m = n;
18                 n = r;
19             }
20             return n;
21         }
22         /**
23                        
24             @param m   
25             @param n   
26             @return n      
27         */
28         const MPOR = function(m,n){
29             //        ,    2       
30             while(m % 2 == 0 && n % 2 == 0){
31                 m = m / 2;
32                 n = n / 2;
33             }
34             //       
35             let temp = null;
36             while((temp = m - n) != n){
37                 //      ,     m,    n
38                 if(temp > n){
39                     m = temp;
40                 } else{
41                     m = n;
42                     n = temp;
43                 }
44             }
45             
46             return n;
47         }
48         document.write(euclid(98,63) + "
"); 49 document.write(MPOR(98,63)); 50 script> 51 body> 52 53 html>
 
転載先:https://www.cnblogs.com/githubMYL/p/8883508.html