Skip to content

Instantly share code, notes, and snippets.

@jshinevar

jshinevar/GCD.java

Last active Jun 10, 2017
Embed
What would you like to do?
Greatest Common Divisor Brute Force Method
import java.util.Scanner;
/**
* Created by James Shinevar on 6/10/17.
* james.shinevar@gmail.com
* http://jamesshinevar.com
*/
public class GCDMain {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter your first number:");
long FirstNumber = input.nextLong();
input.nextLine();
System.out.println("Please enter your second number:");
long SecondNumber = input.nextLong();
input.nextLine();
System.out.println("SlowGCD is: " + SlowGCD(FirstNumber,SecondNumber));
}
private static long SlowGCD(long FirstNumber, long SecondNumber) {
long GCD = 0;
for (long counter = 1; counter < (FirstNumber + SecondNumber); counter++) {
if (FirstNumber % counter == 0) {
if (SecondNumber % counter == 0) {
GCD = counter;
}
}
}
return GCD;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.