简体   繁体   English

找出有 4 个不同质因数的数

[英]Find the number that has 4 distinct prime factors

Get the number n from the user.Then print the nth number that has at-least 4 distinct prime factors.从用户那里获取数字 n。然后打印具有至少 4 个不同质因数的第 n 个数字。 Eg(210 prime factors(2,3,5,7) ) 210 is the first number that is having 4 distinct prime factors the next number is 330(2,3,5,11).例如(210 prime factor(2,3,5,7)) 210 是第一个有 4 个不同素数的数,下一个数是 330(2,3,5,11)。 Input:2 output:330 and Input:3 output:390.I don't know how to do this?I tried to find the prime factors for a number. Input:2 output:330 and Input:3 output:390.I don't know how to do this?我试图找到一个数字的质因数。

 for (int i = 2; i <= number; i++) {
     while (number % i == 0) {
        System.out.print(i + " ");
        number = number / i;
     }
 }
 if (number < 1) 
     System.out.println(number);

But i want to print the nth number that has 4 distinct prime factors.但我想打印具有 4 个不同质因数的第 n 个数字。

You may use the following code:您可以使用以下代码:

public static boolean findPrimeFactors(int n) {
    Set<Integer> primeFactorSet = new HashSet<>();
    while (n % 2 == 0) {
        // here number is even so adding 2
        primeFactorSet.add(2);
        n /= 2;
    }

    // number would be odd in this loop
    for (int i = 3; i <= Math.sqrt(n); i += 2) {
        while (n % i == 0) {
            primeFactorSet.add(i);
            n /= i;
        }
    }

    if (n > 2) {
        primeFactorSet.add(n);
    }

    // true if the unique prime-factors are greater than or equal to 4
    return primeFactorSet.size() >= 4 ? true : false;
}

Now invoke it using:现在使用以下方法调用它:

public static void main(String[] args) {
    List<Integer> primeFactorList = new ArrayList<Integer>();
    // accept this from user
    int n = 2;

    for (int i = 210;; i++) {
        // find prime factors for each number
        if (findPrimeFactors(i)) {
            primeFactorList.add(i);
        }
        if (primeFactorList.size() == n) {
            System.out.println(primeFactorList.get(n - 1));
            break;
        }
    }
}

Explanation:解释:

  1. The loop iterates from 210 till the nth number that has four or more different prime factors.循环从 210 迭代到具有四个或更多不同质因数的第 n 个数字。
  2. For every number that meets the criteria, the method returns true else false.对于满足条件的每个数字,该方法返回真否则假。
  3. Next a check is made to see if the size of the list is equal to the number ( n ) entered by the user.接下来检查列表的大小是否等于用户输入的数字 ( n )。 If its equal the n-1 th index is fetched and the loop is exited.如果它等于第n-1个索引,则获取并退出循环。

One problem with the above method is that it divides by all odd numbers up to the square root of the candidate.上述方法的一个问题是它除以所有奇数,直到候选者的平方根。 It would be better to divide by previously accumulated primes.最好除以先前累积的素数。 That is easily done as follows:这很容易完成,如下所示:

      List<Integer> primes = new ArrayList<>(List.of(2));
      outer:
      for (int i = 3; i < n; i++) {
         int end = (int)Math.sqrt(i);
         for (int k = 0, p = primes.get(0); p <= end; p =
               primes.get(k++)) {
            if (i % p == 0) {
               continue outer;
            }
         }
         primes.add(i);
      }

And move the square root calculation out of the loop.并将平方根计算移出循环。 It is evaluated on every cycle.它在每个周期进行评估。

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

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