Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of NonTrivial Factors in Java

Tags:

java

factors

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 (i.e; 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.P.S: This program is not complete as I am not being able to compare.

like image 280
Bishal Gautam Avatar asked Jan 06 '23 10:01

Bishal Gautam


2 Answers

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);
}
like image 85
Eran Avatar answered Jan 08 '23 00:01

Eran


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++) {
like image 28
Davide Lorenzo MARINO Avatar answered Jan 07 '23 23:01

Davide Lorenzo MARINO