Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class First {
- static int gcd(int a, int b) {
- if (a > b) {
- return gcd(a - b, b);
- } else if (b > a) {
- return gcd(a, b - a);
- } else {
- return a;
- }
- }
- public static void main(String[] args) {
- System.out.println(gcd(15, 3));
- }
- }
- -------------------------------------------
- public class Second {
- static int gcd(int a, int b) {
- if (a > b) {
- return gcd(a - b, b);
- } else if (b > a) {
- return gcd(a, b - a);
- } else {
- return a;
- }
- }
- public static void main(String[] args) {
- System.out.println(gcd(8, 4));
- }
- }
- -------------------------------------------
- public class Third {
- static int gcd(int a, int b) {
- for(int i = Math.min(a, b); i >= 1; i--) {
- if(a % 1 == 0 && b % 1 == 0) {
- return i;
- }
- }
- return 1;
- }
- public static void main(String[] args) {
- System.out.println(gcd(10,5));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement