Note that there are some explanatory texts on larger screens.

plurals
  1. POBig O Notation of a certain algorithm?
    primarykey
    data
    text
    <p>I have the following algorithm that determines the greatest common divisor of two numbers x and y. I need to find the big o notation that describes this algorithm and explain why, but I have no idea how to do this. </p> <p>Could someone please look at my code and explain what type of big oh notation it would be?</p> <pre><code> 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&lt;=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&lt;=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&lt;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&lt;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 } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload