# Java Exercises: Accept a even number from the user and create a combinations that express the given number as a sum of two prime numbers

## Java Basic: Exercise-233 with Solution

Write a Java program that accept a even number (n should be greater than or equal to 4 and less than or equal to 50,000, Goldbach number) from the user and create a combinations that express the given number as a sum of two prime numbers. Print the number of combinations.

Goldbach number: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Since four is the only even number greater than two that requires the even prime 2 in order to be written as the sum of two primes, another form of the statement of Goldbach's conjecture is that all even integers greater than 4 are Goldbach numbers.
The expression of a given even number as a sum of two primes is called a Goldbach partition of that number. The following are examples of Goldbach partitions for some even numbers:
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7 = 5 + 5
12 = 7 + 5
...
100 = 3 + 97 = 11 + 89 = 17 + 83 = 29 + 71 = 41 + 59 = 47 + 53

Sample Solution:

Java Code:

`````` import java.io.BufferedReader;
import java.io.IOException;

public class Main {

public static void main(String[] args) throws NumberFormatException,IOException {
StringBuilder builder = new StringBuilder();
int max = 50000;
System.out.print("Input an even number: ");
boolean[] primes = new boolean[max + 1];
int count = 1;
for (int i = 3; i <= max; i += 2) {
if (!primes[i]) {
count++;
if (i <= Math.sqrt(max)) {
for (int j = i; j <= max / i; j += 2) {
primes[(int) (i * j)] = true;
}
}
}
}
int[] prime = new int[count];
prime = 2;
int count2 = 1;
for (int i = 3; i <= max; i += 2) {
if (!primes[i]) {
prime[count2] = i;
count2++;
}
}
int[] g = new int[max + 1];
for (int i = 0; i <= prime.length; i++) {
for (int j = i; j < prime.length && prime[i] + prime[j] <= max; j++) {
g[prime[i] + prime[j]]++;
}
}

builder.append(g[n]).append('\n');
System.out.print("\nNumber of combinations: ");
System.out.print(builder);

}
}
```
```

Sample Output:

```Input an even number: 100

Number of combinations: 6
```

Pictorial Presentation: Flowchart: Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## Java: Tips of the Day

How to remove leading zeros from alphanumeric text?

Regex is the best tool for the job; what it should be depends on the problem specification. The following removes leading zeroes, but leaves one if necessary (i.e. it wouldn't just turn "0" to a blank string).

```s.replaceFirst("^0+(?!\$)", "")
```

The ^ anchor will make sure that the 0+ being matched is at the beginning of the input. The (?!\$) negative lookahead ensures that not the entire string will be matched.

Test harness:

```String[] in = {
"01234",         // ""
"0001234a",      // "[1234a]"
"101234",        // ""
"000002829839",  // ""
"0",             // ""
"0000000",       // ""
"0000009",       // ""
"000000z",       // "[z]"
"000000.z",      // "[.z]"
};
for (String s : in) {
System.out.println("[" + s.replaceFirst("^0+(?!\$)", "") + "]");
}```

Ref: https://bit.ly/2Qdcl8a