Count Consecutive Primes in Java

/** 
 * This method calculates the number of consecutive prime numbers that 
 * result from a quadratic equation. 
 * Assume the quadratic equation is in this form: x^2 + a*x + b
 * Pass the method the a and b coefficients.
 * 
 * @param a Coefficient of the middle term of the quadratric
 * @param b Coefficient of the rightmost term of the quadratic
 * @return The number of consecutive primes generated by the 
 *  quadratic, starting with x = 0.
 */

    private static int countConsecutivePrimes(int a, int b) {
        int count, value = 0, num = 0;
        count = 0;
        while (true) {
            value = num * num + a * num + b;
            if (Utils.isPrime(value) == true) {
                count++;
            } else {
                break;
            }

            num++;
        }
        return count;
    }

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working