【问题标题】:Number of NonTrivial Factors in JavaJava 中非平凡因素的数量
【发布时间】:2016-09-01 08:11:04
【问题描述】:

我有一个问题要检查总计数是否等于数字的任何因素。我正处于 JAVA 编程的学习阶段。问题如下:

*

Bishal 数是一个数,使得非平凡因子的数量 是数量的一个因素。例如,6 是比沙尔数,因为 6 有两个非平凡因数:2 和 3。(非平凡因数是一个因数 除了 1 和数字)。因此 6 有两个非平凡因子。现在, 2 是 6 的因数。因此非平凡因数的数量是因数 6。因此 6 是比沙尔数。另一个比沙尔数是 30,因为 30 有 2、3、5、6、10、15 作为非平凡因数。因此 30 有 6 不平凡的因素。请注意,6 是 30 的因数。所以 30 是 Bishal 数字。然而,21 不是比沙尔数。非平凡因素 21 是 3 和 7。因此非平凡因素的数量是 2。请注意 2 不是 21 的因数。因此,21 不是比沙尔数。写 一个名为 isBishal 的函数,如果它的整数参数是一个,则返回 1 Bishal 数,否则返回 0。
函数的签名 是 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

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

谁能在这个程序中让我清楚。P.S:这个程序不完整因为我无法比较。

【问题讨论】:

    标签: java factors


    【解决方案1】:

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

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

    【讨论】:

    • OP 要求一个 int 函数,而不是一个布尔值。在第一个示例中,不需要额外的变量计数,您可以使用 factor.size()。
    • @DavideLorenzoMARINO 我错过了那部分要求,使用布尔返回类型更有意义。关于factors.size()的好点。
    • 谢谢!! HashSet 概念对我来说是全新的。第二个对我来说似乎很方便。我已经开始谷歌哈希集。虽然它返回“True”或“False”,但我已经有了这个概念。再次感谢。
    【解决方案2】:

    关于你开始的方式的几点。

    首先:不要使用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++) {
    

    【讨论】:

    • 说到优化,我想你可以取消第二个循环,只需使用divisors.contains(divisors.size())。其次,这样的方法应该返回一个boolean,而不是int
    • @GyroGearless 我听从了你的建议。包含适用于此示例。但是 OP 明确要求返回 0 或 1 的函数,而不是 false 或 true。
    • 非常感谢!!我不知道 Hash Set 的概念,现在我正在研究它。现在我清楚我的问题了。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:
      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 它们。

      【讨论】:

      • 我不明白n 的非平凡因素如何在[2, sqrt(n)] 范围内。即30 的非平凡因素将介于[2, sqrt(30)][2,5.47..] 之间?但是有6 的非平凡因素30[2, 15] 不等。你能解释一下吗?我的印象是一个数字 n[1, sqrt(n)].. 之间有 质数 因数
      • 是的,[1, sqrt(n)] 是关于素数的。
      【解决方案5】:
      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;
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        • 1970-01-01
        • 2013-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多