package com.codegym.task.task14.task1420;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
GCD
*/
public class Solution {
public static void main(String[] args) throws Exception {
// create reader
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// store two numbers
String num1 = reader.readLine();
String num2 = reader.readLine();
if (isPositiveInteger(num1) && isPositiveInteger(num2))
{
// change strings to ints
int int1 = Integer.parseInt(num1);
int int2 = Integer.parseInt(num2);
// call createDivisorArray function
ArrayList<Integer> divisors1 = createDivisorArray(int1);
ArrayList<Integer> divisors2 = createDivisorArray(int2);
// call matchingDivisors function
ArrayList<Integer> matches = matchingDivisors(divisors1, divisors2);
// call gcd function
System.out.println(gcd(matches));
}
}
// create function to make array of divisors
public static ArrayList<Integer> createDivisorArray(Integer num)
{
ArrayList<Integer> divisors = new ArrayList<>();
// use modulo to determine divisors
for (int i = num; i > 0; i--)
{
if (num % i == 0)
{
divisors.add(i);
}
}
return divisors;
}
// create function to check for matching divisors
public static ArrayList<Integer> matchingDivisors(ArrayList<Integer> arr1, ArrayList<Integer> arr2)
{
arr1.retainAll(arr2);
return arr1;
}
// create function to return biggest divisor
public static Integer gcd(ArrayList<Integer> arr1)
{
return arr1.get(0);
}
// create function to check if string can be changed into a positive integer
public static boolean isPositiveInteger(String num)
{
try {
int int1 = Integer.parseInt(num);
return (int1 > 0);
}
catch (NumberFormatException e)
{
System.out.println("Not a number");
return false;
}
}
}package com.codegym.task.task14.task1420;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
GCD
*/
public class Solution {
public static void main(String[] args) throws Exception {
// create reader
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// store two numbers
String num1 = reader.readLine();
String num2 = reader.readLine();
if (isPositiveInteger(num1) && isPositiveInteger(num2))
{
// change strings to ints
int int1 = Integer.parseInt(num1);
int int2 = Integer.parseInt(num2);
// call createDivisorArray function
ArrayList<Integer> divisors1 = createDivisorArray(int1);
ArrayList<Integer> divisors2 = createDivisorArray(int2);
// call matchingDivisors function
ArrayList<Integer> matches = matchingDivisors(divisors1, divisors2);
// call gcd function
System.out.println(gcd(matches));
}
}
// create function to make array of divisors
public static ArrayList<Integer> createDivisorArray(Integer num)
{
ArrayList<Integer> divisors = new ArrayList<>();
// use modulo to determine divisors
for (int i = num; i > 0; i--)
{
if (num % i == 0)
{
divisors.add(i);
}
}
return divisors;
}
// create function to check for matching divisors
public static ArrayList<Integer> matchingDivisors(ArrayList<Integer> arr1, ArrayList<Integer> arr2)
{
// System.out.println(arr2);
// System.out.println(arr1);
arr1.retainAll(arr2);
// System.out.println(arr1);
return arr1;
}
// create function to return biggest divisor
public static Integer gcd(ArrayList<Integer> arr1)
{
return arr1.get(0);
}
// create function to check if string can be changed into a positive integer
public static boolean isPositiveInteger(String num)
{
try {
int int1 = Integer.parseInt(num);
return (int1 > 0);
}
catch (Exception e)
{
System.out.println("Not a number");
return false;
}
}
// create function to determine if number is positive
// public boolean isPositive(String num)
// {
// return (num > 0);
// }
}