简体   繁体   中英

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

If I understand correctly you want to find N prime numbers starting from X. Code should be pretty simple:

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

   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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