Skip to content

Instantly share code, notes, and snippets.

@peter279k
Created December 22, 2015 02:08
Show Gist options
  • Save peter279k/f5f026572b9c21078820 to your computer and use it in GitHub Desktop.
Save peter279k/f5f026572b9c21078820 to your computer and use it in GitHub Desktop.
import java.util.*;
public class main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(input.hasNext()) {
int result = 0;
int N = input.nextInt();
if(N == 0)
break;
for(int i=1;i<N;i++)
for(int j=i+1;j<=N;j++) {
//System.out.println(i + "," + j);
//System.out.println(GCD(i,j));
result += GCD(i,j);
}
System.out.println(result);
}
}
public static int GCD(int num1, int num2) {
if(num2 > num1) {
int temp = num2;
num2 = num1;
num1 = temp;
}
int result = 0;
while(num2 != 0) {
int temp = num1 % num2;
num1 = num2;
num2 = temp;
result = num1;
}
if(result == 0)
return 1;
else
return num1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment