Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- public class Goldbachs {
- // This method decides if number is prime or not and returns true or false.
- public static boolean isPrime(int i) {
- for (int u = 2; u < i; u++) {
- if (i%u==0)
- return false;
- }
- return true;
- }
- public static void main (String[] args) {
- while (true) {
- String s = JOptionPane.showInputDialog("Insert an even number greater "
- + "than 2 and I'll calculate \nwhich primes can be summed up"
- + " to form that number!", JOptionPane.QUESTION_MESSAGE);
- if (s == null)
- break;
- int inputNumber = Integer.parseInt(s);
- String result = "";
- int numberofRows = 0;
- //Does inputNumber meet the requirements?
- if (inputNumber>2 && inputNumber%2==0) {
- //Which prime numbers are canditates to form inputNumber?
- for (int p = 1; p <=(inputNumber/2); p++) {
- //Is there another prime suitable to form inputNumber?
- if ((isPrime(p)) && (isPrime(inputNumber-p))) {
- result = result + p + " + " + (inputNumber-p) + "\n";
- numberofRows = numberofRows + 1;
- }
- }
- if (numberofRows > 30) {
- System.out.println (result);
- }
- else {
- JOptionPane.showMessageDialog(null, result , "Here are your primes!",
- JOptionPane.INFORMATION_MESSAGE);
- }
- }
- else {
- JOptionPane.showMessageDialog(null, "Invalid number, please pick a"
- + " new one.", "Number does not meet the requirements!", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment