简体   繁体   中英

Number of NonTrivial Factors in Java

I've got a problem to check if total count equals to any factor of the number.I'm in learning phase of JAVA Programming. The question is as follows:

*

A Bishal number is a number such that the number of nontrivial factors is a factor of the number. For example, 6 is a Bishal number because 6 has two nontrivial factors : 2 and 3. (A nontrivial factor is a factor other than 1 and the number). Thus 6 has two nontrivial factors. Now, 2 is a factor of 6. Thus the number of nontrivial factors is a factor of 6. Hence 6 is a Bishal number. Another Bishal number is 30because 30 has 2, 3, 5, 6, 10, 15 as nontrivial factors. Thus 30 has 6 nontrivial factors. Note that 6 is a factor of 30. So 30 is a Bishal Number. However 21 is not a Bishal number. The nontrivial factors of 21 are 3 and 7. Thus the number of nontrivial factors is 2. Note that 2 is not a factor of 21. Therefore, 21 is not a Bishal number. Write a function named isBishal that returns 1 if its integer argument is a Bishal number, otherwise it returns 0.
The signature of the function is int isBishal(int n)

*

I can create a function. But I am not getting idea how to check total count with factors. Some parts of my solution is as Follows:

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 Final count (ie; total number of factors) to any factor? If I use factor equals to count, count starts from 1,2,3 and so on. And it may compare count 1 , 2,3 or so on with factors. I need to compare Final count. SO I've put count out of the loop. But then scope of factor is just within if clause. It can't be compared outside loop.

Would anyone please make me clear in this program.PS: This program is not complete as I am not being able to compare.

You have to store the factors you found in order to check if the number of non trivial factors is a factor.

You can use, for example, a 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);
}

EDIT: As khelwood suggested, an alternative to storing the factors is checking at the end of the loop if count is a factor of 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);
}

I will try to guide you in correct direction.

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.

}

A couple of point on the way you started.

First: Don't use double to operate with integers. To know if a number is divisor of another use the remainder operator % (If A % B == 0 , B is a divisor of A ).

Second: You don't need only to know if a number is a divisor, but you need also to hold it. You can use a Set to hold all the divisors. Add it in the if clause.

Third: If you save the divisors in a Set you don't need a count variable to know how many divisors exists. Simply count elements in the divisors Set.

Fourth: once you what divisors you have you can simply loop over the divisors to check if there is a divisor with value equals to the number of divisors.

Here is the code:

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
}

Additional note: other optimization can be done. For example is not needed to loop from 2 to n - 1. No divisor can exist between n / 2 + 1 and n - 1, so you can rewrite the first loop with the following condition:

for (int i = 2; i <= (n / 2) + 1; i++) {
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;
}

All non trivial factors of n are in range [2, n/2] . Here we filter integers from range by predicate and then count them.

public class Demo{
public static void main(String[] args) {
    System.out.println("Result: " + isBishal(21));
}

public static int isBishal(int n) {
    int countFactors = 0;
    int flag = 0;// False case
    for (int i = 2; i < n; i++) {
        if (n % i == 0) {
            countFactors++;
        }
    }
    if (n % countFactors == 0) {
        flag = 1;// True case
    }
    return flag;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM