Created
December 22, 2015 02:08
-
-
Save peter279k/f5f026572b9c21078820 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.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