Created
February 6, 2020 17:09
-
-
Save imprakharshukla/9c5e1d5f70c00f6a26652c4f5a02a7fb to your computer and use it in GitHub Desktop.
Gold Bach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class Q1_2018 { | |
static int n = 0; | |
static int primes[]; | |
public static void main(String[] args) throws IOException { | |
InputStreamReader ir = new InputStreamReader(System.in); | |
BufferedReader br = new BufferedReader(ir); | |
System.out.println("Enter the Number"); | |
n = Integer.parseInt(br.readLine()); | |
primes = new int[n]; | |
if (isValid()) { | |
oddPrimeArray(); | |
//cycling through the array in the most efficient way to pick out all the viable combinations. | |
for (int i = 0; i < primes.length; ++i) { | |
for (int j = i; j < primes.length; ++j) { | |
if (primes[i] + primes[j] == n) | |
System.out.println(primes[i] + " " + primes[j]); | |
} | |
} | |
} | |
else | |
System.out.println("Invalid!"); | |
} | |
static boolean isValid() { | |
return n > 9 && n < 50; | |
} | |
static void oddPrimeArray() { | |
int k = 0; | |
for (int i = 1; i <= n; ++i) { | |
int c = 0; | |
for (int j = 1; j <= i; ++j) { | |
if (i % j == 0) { | |
++c; | |
} | |
} | |
if (c == 2) { | |
//now checking for odd | |
if (i % 2 != 0) { | |
primes[k] = i; | |
++k; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment