Koech C. Gideon
Koech C. Gideon

Reputation: 31

heavy prime number in java

  1. A prime heavy number is defined to be one that is the sum of more than one pair of prime numbers. Recall that a prime number is a number greater than 1 whose only divisors are 1 and itself. For example, 16 is prime heavy because 16=3+13 and 5+11 (note that 3, 5, 11, and 13 are all prime). 24 is prime heavy because 24 = 5+19, 7+17 and 11+13. However, 8 is not prime heavy because 8 = 3+5 but no other pair of primes sums to 8. Write a function named isPrimeHeavy that returns 1 if its argument is prime heavy, otherwise it returns 0. The function signature is int isPrimeHeavy (int n) You may assume that a function named isPrime already exists that returns 1 if its argument is a prime. You can call this function but do not have to write it.

I did this but it cant return a heavy prime..just returns a prime number...

public class Prime {

    public static boolean isPrimeHeavy(int n) {
        if (n <= 1) {
            return false;
        }
        if (n == 2) {
            return true;
        }
        for (int i = 2; i <= Math.sqrt(n) + 1; i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static boolean isPrimeHeavy(int n) {
        if (n <= 1) {
            return false;
        }
        if (n == 2) {
            return true;
        }
        if (n % 2 == 0) {
            return false;
        }
        for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
}

public class PrimeTest {

    public PrimeTest() {
    }

    @Test
    public void testIsPrime() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

        Prime prime = new Prime();
        TreeMap<Long, String> methodMap = new TreeMap<Long, String>();


        for (Method method : Prime.class.getDeclaredMethods()) {

            long startTime = System.currentTimeMillis();

            int primeCount = 0;
            for (int i = 0; i < 1000000; i++) {
                if ((Boolean) method.invoke(prime, i)) {
                    primeCount++;
                }
            }

            long endTime = System.currentTimeMillis();

            Assert.assertEquals(method.getName() + " failed ", 78498, primeCount);
            methodMap.put(endTime - startTime, method.getName());
        }


        for (Entry<Long, String> entry : methodMap.entrySet()) {
            System.out.println(entry.getValue() + " " + entry.getKey() + " Milli seconds ");
        }
    }
}

Upvotes: 2

Views: 852

Answers (3)

LJ2
LJ2

Reputation: 613

if((argument % 2 == 0 && argument > 12) || argument == 10) {
    return 1;
} else {
    return 0;
}

Upvotes: 1

Avi Cohen
Avi Cohen

Reputation: 3414

public class Prime {

    public static boolean isPrimeHeavy(int n) {
        if (n % 2 != 0) {
            return false;
        }
        int found = 0;
        for (int i = n-3; i >= (n/2); i -= 2) {
            if (isPrime(i) && isPrime(n - i)) {
                found++;
                if (found == 2)
                    return true;
            }
        }
        return false;
    }
}

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533530

You can use a single loop to try all the possible first values and you can calculate the second, when you find there is more than one pair, return 1, otherwise return 0.

I have given you this much as a hint because its maths really rather than programming. You will find problems like this at Project Euler. IMHO You shouldn't be expected to know how to solve the maths problem unless you are employed for a maths role, but you should be able to write the code if you are a professional developer.

Upvotes: 5

Related Questions