Reputation: 31
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
Reputation: 613
if((argument % 2 == 0 && argument > 12) || argument == 10) {
return 1;
} else {
return 0;
}
Upvotes: 1
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
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