Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9106637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:23:07+00:00 2026-06-17T02:23:07+00:00

I have the following algorithm that determines the greatest common divisor of two numbers

  • 0

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.

Could someone please look at my code and explain what type of big oh notation it would be?

     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
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:23:08+00:00Added an answer on June 17, 2026 at 2:23 am

    The largest complexity is the two nested for-loops that you have. Big O is order and means it is the complexity relative to the input size. Here your input size is number of divisors which you find in linear time (1 for-loop each) meaning n + n or O(n). The sorting in your example is usually of average complexity of n*log(n). Your nester for-loops are square meaning O(n^2). Your order is then the O(n^2) because this is the largest complexity in computation. We take the largest degree in the polynomial expression that we get of adding all the complexities so O(n^2 + n*log(n) + 2n) which is 2nd degree polynomial and thus ~ O(n^2).

    It should be noted that the order is the larger complexity of space and time. So if memory usage complexity is larger than computational complexity then that takes over.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following string: 0000015700 I need an algorithm that does the following:
I have an algorithm that recursively makes change in the following manner: public static
I have a SP that has the following algorithm. IF <SomeCondition> BEGIN SELECT *
I have the following problem: There is an array that consists of 6 numbers,
I need to come up with an algorithm that does the following: Lets say
I have the following (imperative) algorithm that I want to implement in Haskell: Given
I have the following problem on my homework: Give an O(n+m) algorithm to find
I have written the following algorithm in order to evaluate a function in MatLab
Following the standard instruction for using AES algorithm, I have not been able to
I have following fiddle: http://jsfiddle.net/BFSH4/ As you see there are two issues: The h1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.