Java 中的重要因素数

Number of NonTrivial Factors in Java

我在 JAVA 编程的学习阶段检查总计数是否等于 number.I 的任何因子时遇到问题。题目如下:

*

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)

*

我可以创建一个函数。但我不知道如何用因素检查总数。我的部分解决方案如下:

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

在哪里可以将最终计数(即因子总数)与任何因子进行比较?如果我使用 factor equals 来计数,则计数从 1、2、3 开始,依此类推。它可能会将计数 1 、 2,3 等与因子进行比较。我需要比较最终计数。所以我已经把计数排除在外了。但是 factor 的范围就在 if 子句之内。无法在循环外比较。

谁能告诉我这个program.P.S:这个程序不完整因为我无法比较。

您必须存储找到的因子,以检查非平凡因子的数量是否是一个因子。

例如,您可以使用 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);
}

编辑:正如 khelwood 所建议的,另一种存储因子的方法是在循环结束时检查 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);
}

我会尽量引导你正确的方向。

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.

}

关于您开始的方式的几点说明。

首先:不要使用double来进行整数运算。要知道一个数是否是另一个数的约数,请使用余数运算符 %(如果 A % B == 0BA 的约数)。

其次:你不仅需要知道一个数是不是约数,你还需要掌握它。您可以使用 Set 来保存所有除数。将其添加到if子句中。

第三:如果将除数保存在 Set 中,则不需要计数变量即可知道存在多少除数。简单地计算除数集中的元素。

第四:一旦你有了什么除数,你就可以简单地循环除数来检查是否有一个除数的值等于除数的数量。

代码如下:

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++) {
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 它们。

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;
}

}