2 つの数値 x と y の最大公約数を決定する次のアルゴリズムがあります。このアルゴリズムを説明する大きな o 表記を見つけて、その理由を説明する必要がありますが、これを行う方法がわかりません。
誰かが私のコードを見て、それがどのタイプの大きなああ表記になるか説明してもらえますか?
public void question1(int x, int y){
ArrayList divisorx = new ArrayList(); //the list of divisors of number x
ArrayList divisory = new ArrayList();//divisors of number y
ArrayList answerSet = new ArrayList();//the common divisors from both
//divisorx and divisory
for(int i=1; i<=x; i++){//this loop finds the divisors of number x and
//adds them to divisorx
double remainder = x%i;
if(remainder==0){
//i is a divisor
divisorx.add(i);
}
}
for(int i2=1; i2<=y; i2++){//this loop finds the divisors of number y
//and adds them to divisory
double remainder2 = y%i2;
if(remainder2==0){
//i2 is a divisor
divisory.add(i2);
}
}
int xsize = divisorx.size();
int ysize = divisory.size();
for(int i=0; i<xsize; i++){//this massive loop compares each element of
//divisorx to those of divisory to find common divisors. It adds those common
//divisors to the arraylist answerSet
for(int j=0; j<ysize; j++){
if(divisorx.get(i)==divisory.get(j)){
//common divisor has been found
//add it to an answer array
answerSet.add(divisorx.get(i));
}
}
}
Collections.sort(answerSet);//sorts the answerSet from smallest to greatest
Object gcd = answerSet.get(answerSet.size()-1);//get the last element of the
//arraylist, which is the gcd
System.out.print("Your Answer: "+gcd);//print out the greatest common divisor
}