Created
August 17, 2013 13:39
-
-
Save fsubal/6256905 to your computer and use it in GitHub Desktop.
Just a simple implementation of Euclidean Algorithm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.math.*; | |
public class euclid{ | |
public static void main(String[] args) throws IOException{ | |
String[] strar=new String[2]; | |
try{ | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
System.out.print("Input two numbers here:"); | |
String str = br.readLine(); | |
br.close(); | |
//Any separator is allowed. | |
str=str.replaceFirst("[^0-9]",","); | |
str=str.replaceAll("[[^0-9]&&[^,]]",""); | |
strar=str.split(",",2); | |
//No need to input in descending or ascending order. | |
int[] parin = new int[2]; | |
int a=0; | |
int b=0; | |
parin[0]=Integer.parseInt(strar[0]); | |
parin[1]=Integer.parseInt(strar[1]); | |
a=Math.max(parin[0],parin[1]); | |
b=Math.min(parin[0],parin[1]); | |
int r=a%b; | |
while(r!=0){ | |
a=b; | |
b=r; | |
r=a%b; | |
} | |
if(b==1){ | |
System.out.println("Coprime."); | |
}else{ | |
System.out.println("The GCD is "+b+"."); | |
} | |
}catch(IOException e){ | |
System.out.println("Exception :"+e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment