Home > Blockchain >  how to find number with most divisor from array in jaba
how to find number with most divisor from array in jaba

Time:02-26

i got some problem someone of with really helped me but i got a code who print all of divisor from array, but i tried to print a number with most divisor for ex. array[1,2,3,4,5] and i want to print that the number with most divisor is 4 ( 1,2,4)

    public static class Main {
    public static void main(String[] args) {

      
        System.out.println(getNumWithMaxDivisors(numbers));
    }
    static int getNumDivisors(int n) {
    int noOfDivisors = 0;
    for (int i = 1; i <= n / 2; i  ) {
        if (n % i == 0) {
            System.out.print(i   " ");
            noOfDivisors  ;
        }
    }
    return noOfDivisors;
}

static int getNumWithMaxDivisors(int[] numbers) {
    int currentMaxDivisors = 0;
    int numWithMaxDivisors = numbers[0];
    for (int i = 0; i < numbers.length; i  ) {
        int numDivisors = getNumDivisors(numbers[i]);
        if (numDivisors > currentMaxDivisors) {
            numWithMaxDivisors = numbers[i];
        }
    }
    return numWithMaxDivisors;
}

}

Code looks that, do u know where is a problem ?

CodePudding user response:

The problem is that inside of your getNumWithMaxDivisors() method, you are not redefining the current number of max divisors. To fix this, you can update it inside of the if statement as so:

static int getNumWithMaxDivisors(int[] numbers) {
    int currentMaxDivisors = 0;
    int numWithMaxDivisors = numbers[0];
    for (int i = 0; i < numbers.length; i  ) {
        int numDivisors = getNumDivisors(numbers[i]);
        if (numDivisors > currentMaxDivisors) {
            currentMaxDivisors = numDivisors; //ADD THIS LINE
            numWithMaxDivisors = numbers[i];
        }
    }
    return numWithMaxDivisors;
}

Input:

int[] numbers = {1,2,3,4,5};
System.out.println(getNumWithMaxDivisors(numbers));

Output:

4

Side Note: You could just as well start your for loop at i = 2 in your getNumDivisors() method, since every number is divisible by 1, so there is no point in checking it. This just saves you a bit of time!

I hope this helped! Please let me know if you need any further clarifications or details :)

CodePudding user response:

add this line of code currentMaxDivisors = numDivisors; inside your if-statement like so:

static int getNumWithMaxDivisors(int[] numbers) {
    int currentMaxDivisors = 0;
    int numWithMaxDivisors = numbers[0];
    for (int i = 0; i < numbers.length; i  ) {
        int numDivisors = getNumDivisors(numbers[i]);
        if (numDivisors > currentMaxDivisors) {
            currentMaxDivisors = numDivisors; //here this is missing
            numWithMaxDivisors = numbers[i];
        }
    }
    return numWithMaxDivisors;
}

CodePudding user response:

THANK U, both of u, it works but also i had to remowe this line

public static int getNumDivisors(int n) {
    int noOfDivisors = 0;
    for (int i = 1; i <= n; i  ) {
        if (n % i == 0) {
            noOfDivisors  ;
            System.out.print(i   " "); (THIS LINE)

        }
    }
    return noOfDivisors;
}
  • Related