简体   繁体   English

寻找素数

[英]Find Prime Numbers

I am trying to take two arguments from the command line, the first number is the starting point, and the second one is how many prime numbers should be found after that.我试图从命令行中取两个 arguments,第一个数字是起点,第二个是之后应该找到多少个素数。 I need to print the prime numbers found as many times as the second command argument says.我需要打印与第二个命令参数所说的一样多的素数。 I cannot figure out how to make it run the correct amount of times, and after that find the prime number.我不知道如何让它运行正确的次数,然后找到质数。 Here is what I have tried:这是我尝试过的:

int values = Integer.parseInt(args[0]);   
int loopAmount = Integer.parseInt(args[1]);

for (int i = 2; i <= loopAmount; i++) {
    loopAmount++;
    if (values % i != 0) {
        values++;
        System.out.println(i);
    }
}

Your main loop should be something like this: 您的主循环应如下所示:

int start = Integer.parseInt(args[0]);

int count = Integer.parseInt(args[1]);

for (int candidate = start, i = 0; i < count; ++candidate) {
    if (isPrime(candidate)) {
        i++;
        System.out.println(candidate);
    }
}

I replaced the variable names to make them more meaningful about their purpose. 我替换了变量名,以使它们对它们的用途更有意义。

Inside the loop, the isPrime method is something you'll have to implement: if the parameter it receives is a prime, return true , otherwise false . 在循环内部,必须实现isPrime方法:如果它接收的参数是素数,则返回true ,否则返回false

If I understand correctly you want to find N prime numbers starting from X. Code should be pretty simple: 如果我理解正确,那么您想找到从X开始的N个素数。代码应该非常简单:

int X = Integer.parseInt(args[0]);
int N = Integer.parseInt(args[1]);
int C = 0;
while (C < N)
{
    for(int i=2; i< X; i++)
    {
        if(X % i == 0){
            X++;
            continue;
        }
    }
    System.out.println(X);
    X++;
    C++;
}

An optimized version : 优化版本:

// cache already found primes
final List<Integer> primes = new ArrayList<>();

/**
 * Find {@code count} prime numbers starting at {@code start} inclusive
 */
public void findPrimes(int start, int count) {
    for (int i = 2; count > 0; i++) {
        if (isPrime(i) && i >= start) {
            System.out.println(i);
            count--;
        }
    }
}

private boolean isPrime(final int i) {
    int sqrt = (int)Math.sqrt(i);
    for (int prime : primes) {
        if (i % prime == 0) {
            return false;
        }
        if (prime > sqrt) {
            break;
        }
    }
    primes.add(i);
    return true;
}
  1. We really need to check for divisors only up to sqrt. 我们真的需要检查除数最多不超过sqrt的除数。
  2. We really need to find only prime divisors since any number can be written as a product of prime numbers . 我们真的只需要找到素数除数,因为任何数字都可以写成素数的乘积

solution解决方案

#include <iostream>
using namespace std;

int main() {
    int i, n;
    bool isPrime = true;

    cout << "Enter a positive integer: ";
    cin >> n;

    // 0 and 1 are not prime numbers
    if (n == 0 || n == 1) 
    {
        isPrime = false;
    }
    else
    {
         //algorithm to check for prime numbers
        for (i = 2; i <= n / 2; ++i) 
        {
            if (n % i == 0) 
            {
               isPrime = false;
               break;
            }
         }
    }
    if (isPrime)
        cout << n << " is a prime number";
    else
        cout << n << " is not a prime number";

    return 0;
}

   

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

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