ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Count Consecutive Primes in Java

Updated on May 8, 2009
/** 
 * 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;
    }

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article