The problem is to check a random number n may be the sum of two random primes. For instance,
if n = 34, the possibilities can be (3 + 31), (5 + 29), (17 + 17) ...
So far, I managed to save the primes in the array, but I have no idea how I could check if n is the sum of two primes.
This is part of my code:
public static void primeNumbers(int n) { int i = 0, candidate = 2, countArray = 0, countPrime = 0; boolean flag = true; while (candidate <= n) { flag = true; for (i = 2; i < candidate; i++) { if ((candidate % i) == 0) { flag = false; break; } } if (flag) { countPrime++; } candidate++; } int[] primeNumbers = new int[countPrime]; while (candidate <= n) { flag = true; for (i = 2; i < candidate; i++) { if ((candidate % i) == 0) { flag = false; break; } } if (flag) { primeNumbers[countArray] = candidate; } candidate++; countArray++; } for (i = 0; i <= primeNumbers.length; i++) { } }
At first, I calculated how many primes are between 1-n, so I can declare and initialize my array for primes. Then I store the primes in an array. But now I have no idea how I could check if n sum of two prime numbers.
source share