Skip to content

Instantly share code, notes, and snippets.

@jshinevar
Last active June 10, 2017 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jshinevar/db2d8c308bc4a556c350d8c25d741260 to your computer and use it in GitHub Desktop.
Save jshinevar/db2d8c308bc4a556c350d8c25d741260 to your computer and use it in GitHub Desktop.
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