greatest common divisor

Solutions on MaxInterview for greatest common divisor by the best coders in the world

showing results for - "greatest common divisor"
Theo
07 Jun 2020
1int gcdByBruteForce(int n1, int n2) {
2    int gcd = 1;
3    for (int i = 1; i <= n1 && i <= n2; i++) {
4        if (n1 % i == 0 && n2 % i == 0) {
5            gcd = i;
6        }
7    }
8    return gcd;
9}
Tomas
25 Sep 2020
1/* Function using "Euclidian Algorithm" to recursively find the 
2greatest common divisor/factor (GCD/GCF) of 2 positive numbers*/
3const gcf = function (small, large) {
4    let r = large % small;
5    if (r == 0)
6        return small;
7    else
8        return gcf(r, small);
9}
Silvia
12 Nov 2020
1import java.util.Scanner;
2public class GCDExample3 {
3
4    public static void main(String[] args) {
5
6        int num1, num2;
7        
8        //Reading the input numbers
9        Scanner scanner = new Scanner(System.in);
10        System.out.print("Enter first number:");
11        num1 = (int)scanner.nextInt();
12        
13        System.out.print("Enter second number:");
14        num2 = (int)scanner.nextInt();
15        
16        //closing the scanner to avoid memory leaks
17        scanner.close();
18        while (num1 != num2) {
19        	if(num1 > num2)
20                num1 = num1 - num2;
21            else
22                num2 = num2 - num1;
23        }
24
25        //displaying the result
26        System.out.printf("GCD of given numbers is: %d", num2);
27    }
Marco
11 Nov 2020
1static int gcd(int a, int b)
2    {
3      if (b == 0)
4        return a;
5      return gcd(b, a % b); 
6    }
7     
queries leading to this page
how to find the greatest common factor of javathe greatest common divisor javagreatest common factor definitiongreatest common divisor recursion javahow find gcdexercise about find the greatest common divisorhow to find greatest common factorcode problem greatest common divisor in javahow to find greatest common divisorfind greatest common divisor using recursiongcd cod in javahow to get the gcf of two numbers in javajava greatest common divisorgreatest common divisorgreatest common divisor java recursionhow to find gcdwhat is a greatest common divisor javafind greatest common divisorgreatest common factoringgreatest common divisor vs greatest common factorgreatest common factor algorithmhow to find the greatest common denominator in javahow to find the greatest common factorhow to find gcd with javahow to calculate greatest common divisorgcd finding algorithm javamath get common devisor javagreatest common denominator javagreatest common divisor algorithmget greatest common divisor in javamy gcd javawrite a javafx program that accepts two numbers as shown in the picture below when you click on a button you compute and find the gcd of two numbers what does gcd of two vertices meanjava divisor algorithmjava greatest common divisor recursionthe greatest common divisorgcd java programgreatest common divisor of two numbershow to find the greatest common factor on javagreatest common divisor 28gcd 29 math problem in gcd 2c we are trying to find the largest number that can go into both numbers rsursive javagcd in java functionproperties of greatest common divisorfind greatest common factorwhat is the greatest common divisorjava gcd functionwhat is greatest common divisorgcd question javahighest common factorcommon greatest divisor javagreatest common divisor finder javaprogram to find greatest common divisor in javawhat are the common preservativesgcf java solutiongreatest common factor divisor javahow to create a greatest common divisor in javagcd 28 29 3a greatest common divisor of the elements javahow to find the greatest common denominator of a fraction in javawhere is greatest common divisor usedgreatest common divisor java programgcdfind greatest common divisor in javajava recursive greatest common divisorgcd in javagreatest common factor recursionhow to find the greatest common divisor of two numbers javacalculate gcd of two numbers javagcd javagreatest common multiple javagreatest common divisor in javajava find greatest common factorgreatest common divisor 28gcd 29 math problem in gcd 2c we are trying to find the largest number that can go into both numbers javaeuclidean greatest common divisorgcd java codefind greatest common divisor javagreatest common divisor findergreatest common divisor algorithm javajava gcdhow to find the greatest common divisorfinding the highest common factorjava int gcdhow to find the greatest common divisor javamaximum common divisor javahow to find the greatest common divisor in javagcd of two numbers meansgcd command javagreatest common divisor formularecursive greatest common divisorgreatest common divosor javagcd formula gcd in javahow do you find the greatest common divisor in java 3fgcd number in javagreatest common divisor javafactor out the greatest common factor recusrive greatest common divisior javagreatest common divisor java baeldunggreatest common divisor recursionwhat is the formula for getting the gcf of two numbers in javagcd algorithm javajava find highest common factorfastest method to find gcd of two numbershow to find the greatest common factor of two numbers in javagreatest common divisor