【问题标题】:Prime factorization in a program for class类程序中的素数分解
【发布时间】:2016-11-05 22:37:01
【问题描述】:
import java.util.Scanner;

public class JavaApplication1 {
  public static void main(String[] args) {
    Scanner kboard = new Scanner(System.in);
    int n = 0;
    int i = 1;

    System.out.println("Enter a positive number");
    n = kboard.nextInt();
    System.out.print("The Prime Factors of " + n + " are : ");
    value = 2;
    while (n > 1) {
        i = 1;
        if (n % i != 0){
           i = 1;
           i=i+1;
           if(n % i == 0){
               System.out.println(" "+ i);
           }
        }
        else {
            System.out.print("1 and " + n);
            break;
        }
    }

  }
}

这是我的程序,我大约一个月前开始编码,但程序只给出 1 和数字作为输出,而不是主要因素。

【问题讨论】:

  • 一旦你发现 n 可以被 i 整除,你在哪里减少 n?你也应该从2开始i

标签: java primes


【解决方案1】:

因为这个原因,程序总是给出 1 作为输出:

i = 1;
if (n % i != 0) {
    // ...
} else {
    System.out.print("1 and " + n);
    break;
}

由于i = 1n % i != 0 为假,因为任何n 模块1 将是0。 因此,else 块总是被执行并且你立即跳出while 循环。

即使你解决了这个问题,这个程序还有很多其他问题:

  • 条件n > 1 是无意义的,因为n 在此循环中永远不会改变,因此条件始终为true
  • 检查% 1 毫无意义,因为每个数字都除以1。您应该从2 开始检查。

通过微小的改进,可以改进循环以查找因子:

List<Integer> factors = new ArrayList<>();
for (int i = 2; i <= Math.sqrt(n); i++) {
    if (n % i == 0) {
        factors.add(i);
    }
}

但这还不够好。 这将找到所有因素,而不仅仅是主要因素。 例如,对于数字 40,它将找到 2, 4, 5, 8, 10, 其中只有 2 和 5 是素数。 一个简单的解决方案是添加另一种名为isPrime 的方法,用于检查数字是否为素数。

【讨论】:

    【解决方案2】:

    这应该为您做一个简单的技巧: 只需根据用户输入设置数字即可。

    List<Integer> result = new ArrayList<>();
    
                // Take out the 2s.
                while (num % 2 == 0)
                {
                    result.Add(2);
                    num /= 2;
                }
    
                // Take out other primes.
                int factor = 3;
                while (factor * factor <= num)
                {
                    if (num % factor == 0)
                    {
                        // This is a factor.
                        result.Add(factor);
                        num /= factor;
                    }
                    else
                    {
                        // Go to the next odd number.
                        factor += 2;
                    }
                }
    
                // If num is not 1, then whatever is left is prime.
                if (num > 1) result.Add(num);
    
                return result;
    

    【讨论】:

      【解决方案3】:

      如果你想得到所有的素数,你可以试试:

          int i = 2;
          while (n > 1) {
              if (n % i == 0) {
                  System.out.print(" " + i);
                  while ( n % i == 0 ) {
                      n /= i;
                  }
              }
              i++;
          }
      

      如果n = 960 打印2 3 5

      如果n = 11 打印11

      你怎么确定你不考虑一个非素数?

      • 第二个while 循环尽可能多地划分数字。所以,这个数字不会被 4 整除,因为如果可以的话,它已经被 2 整了两次了。

      如果您还想获得这些质因数的指数,可以使用0 初始化一个变量,为每个除法递增,然后将其打印到标准输出:

          int i = 2;
          while (n > 1) {
              if (n % i == 0) {
                  System.out.print(" " + i);
                  int p = 0;
                  while ( n % i == 0 ) {
                      n /= i;
                      p++;
                  }
                  System.out.print("^"+p);
              }
              i++;
          }
      

      如果n = 98 打印2^1 7^2

      【讨论】:

        猜你喜欢
        • 2011-05-15
        • 1970-01-01
        • 1970-01-01
        • 2015-01-28
        • 1970-01-01
        • 2020-03-19
        • 1970-01-01
        • 1970-01-01
        • 2016-03-04
        相关资源
        最近更新 更多