Advertisement
16112

НОД - 3 начина

Mar 17th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. public class First {
  2.     static int gcd(int a, int b) {
  3.         if (a > b) {
  4.             return gcd(a - b, b);
  5.         } else if (b > a) {
  6.             return gcd(a, b - a);
  7.         } else {
  8.             return a;
  9.         }
  10.     }
  11.  
  12.     public static void main(String[] args) {
  13.         System.out.println(gcd(15, 3));
  14.     }
  15. }
  16.  
  17. -------------------------------------------
  18.  
  19. public class Second {
  20.     static int gcd(int a, int b) {
  21.         if (a > b) {
  22.             return gcd(a - b, b);
  23.         } else if (b > a) {
  24.             return gcd(a, b - a);
  25.         } else {
  26.             return a;
  27.         }
  28.     }
  29.  
  30.     public static void main(String[] args) {
  31.         System.out.println(gcd(8, 4));
  32.     }
  33. }
  34.  
  35. -------------------------------------------
  36.  
  37. public class Third {
  38.     static int gcd(int a, int b) {
  39.         for(int i = Math.min(a, b); i >= 1; i--) {
  40.             if(a % 1 == 0 && b % 1 == 0) {
  41.                 return i;
  42.             }
  43.         }
  44.         return 1;
  45.     }
  46.     public static void main(String[] args) {
  47. System.out.println(gcd(10,5));
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement