/** Find gcd for integers m and n */ public static int gcd(int X, int Y) { int gcd = 1; if (X % Y == 0) return Y; for (int k = Y / 2; k >= 1; k--) { if (X % k == 0 && Y % k == 0) { gcd = k; break; } } return gcd; }
/** Find gcd for integers m and n */ public static int gcd(int X, int Y) { int gcd = 1; if (X % Y == 0) return Y; for (int k = Y / 2; k >= 1; k--) { if (X % k == 0 && Y % k == 0) { gcd = k; break; } } return gcd; }
Chapter11: Exception Handling
Section: Chapter Questions
Problem 8RQ
Related questions
Question
I want the
the codes is written in java
Expert Solution
Introduction
GCD (Greatest Common Divisor) of two numbers is the largest number that divides both of them.
Algorithm/Process used:
The algorithm is to Find all prime numbers of both numbers, then find the intersection of all factors present in both numbers. Finally, return the product of elements in the intersection.
Step by step
Solved in 2 steps
Recommended textbooks for you
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning