Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Maximum common divisor

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Maximum common divisor

    Hello,

    I have a problem. I am a beginner in java.
    How should I make an algorithm for finding the max common divisor between 2 numbers?

    I mean, how the code goes and why is it coded like that.
    Thanks!
    Last edited by AdriCORD.97; October 1st, 2020 at 02:29 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,112
    Thanks
    53
    Thanked 2,618 Times in 2,568 Posts

    Default Re: Maximum common divisor

    Can you post the details of the algorithm that you are trying to write code for? Once there is a good algorithm, then work on writing the code for it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Maximum common divisor

    public class GreatestCommonDivisor {
    public static void main(String[] args) {
    System.out.println(getGreatestCommonDivisor(20,10) );

    }
    public static int getGreatestCommonDivisor(int first, int second){
    if(first < 10 || second < 10){
    return -1;
    }
    int divisor = 1;
    if(first > second){
    divisor = first % second;
    }
    if(second > first){
    divisor = second % first;
    }


    return divisor;
    }
    }



    The exercise in the course i'm studying says as a tip to use a while or for loop, and that is the problem. I've read other threads about greatest common divisors and in their code demonstrations the loops have an array keyword. I don't know about such thing. The professor never teached about arrays or other keywords like that, so I'm lost. You can see there that the if statements where the remainder operators are are wrong, since 20 % 10 is 0 and 0 is not a divisor. Would you please take your time to explain arrays to me, or is it too complicated for them to be explained here?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,112
    Thanks
    53
    Thanked 2,618 Times in 2,568 Posts

    Default Re: Maximum common divisor

    Where is the description of the algorithm that you are trying to write the code for?
    It is always a bad idea to start by writing code before you have a working algorithm.

    http://www.java-forums.org/misc.php?do=bbcode#code
    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Maximum common divisor

    public class GreatestCommonDivisor {
        public static void main(String[] args) {
            System.out.println(getGreatestCommonDivisor(20,10));
     
        }
        public static int getGreatestCommonDivisor(int first, int second){
            if(first < 10 || second < 10){
                return -1;
            }
            int divisor = 1;
            if(first > second){
                divisor = first % second;
            }
            if(second > first){
                divisor = second % first;
            }
     
     
            return divisor;
        }
    }


    --- Update ---

    What do you mean by writing code before having a working algorithm? I'm sorry, I'm new to this

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,112
    Thanks
    53
    Thanked 2,618 Times in 2,568 Posts

    Default Re: Maximum common divisor

    An algorithm is a sort of blueprint of what the code will do. No one builds a house or a car without a blueprint describing what the end product looks like. The same with a computer program. Don't try writing the code until there is a design for how the code should work to solve the problem.

    What is the purpose of testing if first or second is less than 10?
    What the purpose of this statement: divisor = first % second;
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Maximum common divisor

    The exercise says that the numbers should be equal or greater than 10.
    And the statement: divisor = first % second; is using an algorithm I read in wikipedia. It is wrong I see.

    I'll work on an algorithm now and consult later if I need to. Thank you!

Similar Threads

  1. Trying to make a largest divisor solver.
    By SirMrNick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2018, 05:17 PM
  2. Maximum size of Collections
    By dicdic in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2014, 07:59 PM
  3. code for finding the maximum in a tree
    By Unobona in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2013, 08:49 AM
  4. Maximum length of a string
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 6th, 2012, 09:47 AM
  5. Recursion, arrays and greatest common divisor
    By GalBenH in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2012, 12:46 AM

Tags for this Thread