Number of non-trivial factors in Java

I have a problem to check if the total score is equal to any coefficient of the number. I am participating in the JAVA Programming training phase. The question is this:

*

The Bishal number is such a number that the number of non-trivial factors is a factor of the number. For example, 6 is the Bishal number, because 6 has two non-trivial factors: 2 and 3. (A factor other than 1 and a number is a non-trivial factor). Thus, 6 has two non-trivial factors. Now, 2 is a factor of 6. Thus, the number of non-trivial factors is a factor of 6. Therefore, 6 is the Bishal number. Another Bishal number - 30 30 has 2, 3, 5, 6, 10, 15 as non-trivial factors. Thus, 30 has 6 non-trivial factors. Note that 6 is a factor of 30. Thus, 30 is the Bishal Number. However, 21 is not a Bishal number. Non-trivial factors 21 are equal to 3 and 7. Thus, the number of non-trivial factors is 2. Note that 2 is not a factor of 21. Therefore, 21 is not a Bishal number.Write an isBishal function that returns 1 if its integer argument is Bishal, otherwise it will return 0.
Function Signature is int isBishal (int n)

*

I can create a function. But I do not understand how to check the total with factors. Some parts of my solution correspond to the following:

public static int isBishal(int n){
   int count=0;   //for number of factor of entered number n  
   for (int i=2; i<n; i++){ //for excluding 1 and itself in factors list
            double result=(double)n/i;
            if(result==Math.ceil(result)){
                int factor=(int) result; //to check factor(one can use reminder 0 case)
                count++;
            } //closing if clause
      } //closing for loop

Where can I compare the final score (i.e. the total number of factors) with any factor? If I use a coefficient equal to the counter, the count starts with 1,2,3 and so on. And that can compare the score of 1, 2,3 or so with factors. I need to compare Final count. I set the score from the cycle. But then the factor scale is only in the if condition. You cannot compare the outer loop.

Someone please let me understand this program .PS: This program is not complete because I can not compare.

+4
source share
4 answers

, .

: double . , , % (If A % B == 0, B - A).

-: , , . Set . if.

-: , count, , . .

-: , , , , .

:

public static int isBishal(int n){
   Set<Integer> factors = new HashSet<>();  // Create a Set for factors 
   for (int i = 2; i < n; i++) { 
       if (n % i == 0) {   // CHeck if i is factor of n using %
           factors.add(i);  // If i is a factor add it to factors
       }
   }
   if (factors.contains(factors.size())) {
        return 1;   // If a factor is equal to the number of factors return 1
   }
   return 0;   // If none factor equals number of divisors return 0
}

: . , 2 n - 1. n/2 + 1 n - 1, :

for (int i = 2; i <= (n / 2) + 1; i++) {
+2

, , .

, , HashSet:

public static boolean isBishal(int n) { // I changed the return type to boolean
    int count=0; 
    Set<Integer> factors = new HashSet<>();
    for (int i=2; i<n; i++){
        if (n % i == 0) { // note this is a simpler way to check if i is a factor of n
            factors.add(i);
            count++;
        }
    }
    return factors.contains(count);
}

: , , count n:

public static boolean isBishal(int n) { 
    int count=0; 
    for (int i=2; i<n; i++){
        if (n % i == 0) {
            count++;
        }
    }
    return (count > 1) && (n % count == 0);
}
+5

.

public static int isBishal(int n){

   int count = 0;  
   for (int i = 2; i < n; i++){      

        if(n % i == 0) {
            // TODO: We found a factor, insert it in some data structure e.g. in stack or (resizable) array          
            count++;
        }

   } 

  // TODO: loop through the array (or data structure you used) where you stored factors, and check if any of them matches count.
  //       If none of them match count, return 0, otherwise 1.

}
+2
public static boolean isBishal(int n) {
    long factors = IntStream.rangeClosed(2, n/2)
            .filter(x -> n % x == 0)
            .count();
    return factors != 0 
           && n % factors == 0;
}

n [2, n/2]. filter , count .

0

Source: https://habr.com/ru/post/1653274/


All Articles