繁体   English   中英

Java中非特权因素的数量

[英]Number of NonTrivial Factors in Java

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

*

Bishal数是一个数字,使得非平凡因子的数量是该数字的一个因子。 例如,6是Bishal数,因为6具有两个非平凡因子:2和3.(非平凡因子是1以外的因子和数字)。 因此,6有两个非平凡因素。 现在,2是因子6.因此,非平凡因子的数量是因子6.因此,6是Bishal数。 另一个Bishal数是30,因为30有2,3,5,6,10,15作为非平凡因素。 因此30有6个非平凡因素。 注意6是因子30.所以30是Bishal数。 然而,21不是Bishal数字。 21的非平凡因子是3和7.因此,非平凡因子的数量是2.注意,2不是21的因子。因此,21不是Bishal数。 编写一个名为isBishal的函数,如果其整数参数是Bishal数,则返回1,否则返回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开始,依此类推。 并且它可以将计数1,2,3左右与因子进行比较。 我需要比较最终计数。 所以我把数量排除在外。 但是因子的范围恰好在if子句中。 它无法在外循环中进行比较。

有谁请在这个程序中让我明白.PS: 这个程序不完整,因为我无法比较。

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

例如,您可以使用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所建议的那样,存储因子的另一种方法是在循环结束时检查如果countn的因子:

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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