Skip to content

Instantly share code, notes, and snippets.

@midorikocak
Created November 17, 2017 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save midorikocak/ecb05e8b600b6d29ae7968ee26ac35c8 to your computer and use it in GitHub Desktop.
Save midorikocak/ecb05e8b600b6d29ae7968ee26ac35c8 to your computer and use it in GitHub Desktop.
Amazon Practice 2
// IMPORT LIBRARY PACKAGES NEEDED BY YOUR PROGRAM
// SOME CLASSES WITHIN A PACKAGE MAY BE RESTRICTED
// DEFINE ANY CLASS AND METHOD NEEDED
// CLASS BEGINS, THIS CLASS IS REQUIRED
import java.util.*;
class GCD
{
// METHOD SIGNATURE BEGINS, THIS METHOD IS REQUIRED
public int generalizedGCD(int num, int[] arr)
{
if(arr.length==0) return 0;
List<Integer> commonDivisors = primeDivisors(arr[0]);
for(int i=1; i<arr.length; i++){
commonDivisors.retainAll(primeDivisors(arr[i]));
}
int result = 1;
for(int j=0; j<commonDivisors.size(); j++){
result *= commonDivisors.get(j);
}
return result;
}
public List<Integer> primeDivisors(int num){
List<Integer> divisors = new ArrayList<Integer>();
divisors.add(1);
if(num%2 == 0) divisors.add(2);
for(int i=3; i<Math.sqrt(num); i+=2){
if(num%i == 0) divisors.add(i);
}
return divisors;
}
public int smallestPrimeDivisor(int num){
if(num%2 == 0) return 2;
for (int i=3;i<Math.sqrt(num);i+=2)
{
if (num%i==0) return(i);
}
return(num);
}
// METHOD SIGNATURE ENDS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment