From aeeb26fef4aea0c7d422f0d92a598aee96daa74d Mon Sep 17 00:00:00 2001 From: BILL_SARAN Date: Sat, 11 Jan 2025 20:25:24 +0200 Subject: [PATCH 1/3] Added the Goldbach's Conjecture algorithm --- .../maths/GoldbachConjecture.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/GoldbachConjecture.java diff --git a/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java b/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java new file mode 100644 index 000000000000..e277bd41b0c2 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java @@ -0,0 +1,57 @@ +package com.thealgorithms.maths; + +import java.util.Scanner; + +import static java.lang.String.format; + +/** + * This is a representation of the unsolved problem of Goldbach's Projection, according to which every + * even natural number greater than 2 can be written as the sum of 2 prime numbers + * More info: https://en.wikipedia.org/wiki/Goldbach%27s_conjecture + * @author Vasilis Sarantidis (https://github.com/BILLSARAN) + */ + +public final class GoldbachConjecture { + private GoldbachConjecture(){ + } + + /** + * Checks whether a number is prime or not + * @param n the input number + * @return true if n is prime, else return false + */ + private static boolean isPrime(int n){ + int i; + if(n <= 1 || (n%2 == 0 && n!=2)){ + return false; + } + else { + for(i = 3; i2) { + for(int i=0;i<=n/2 && flag==0;i++) + if(isPrime(i)) + if(isPrime(n-i)) + { + System.out.println(format("%d+%d=%d", i, n-i, n)); + flag=1; + } + } + else + System.out.println("Wrong Input"); + } + +} \ No newline at end of file From e8aada56ea02d56bfba7cad8bcfbbe3961fe52dd Mon Sep 17 00:00:00 2001 From: BILL_SARAN Date: Sat, 11 Jan 2025 20:31:29 +0200 Subject: [PATCH 2/3] Added the Goldbach's Conjecture algorithm --- .../maths/GoldbachConjecture.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java b/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java index e277bd41b0c2..036ef2876235 100644 --- a/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java +++ b/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java @@ -12,7 +12,7 @@ */ public final class GoldbachConjecture { - private GoldbachConjecture(){ + private GoldbachConjecture() { } /** @@ -20,21 +20,21 @@ private GoldbachConjecture(){ * @param n the input number * @return true if n is prime, else return false */ - private static boolean isPrime(int n){ + private static boolean isPrime(int n) { int i; - if(n <= 1 || (n%2 == 0 && n!=2)){ + if(n <= 1 || (n % 2 == 0 && n != 2)) { return false; } else { - for(i = 3; i2) { - for(int i=0;i<=n/2 && flag==0;i++) + for(int i = 0; i <= n/2 && flag == 0; i++) if(isPrime(i)) - if(isPrime(n-i)) + if(isPrime(n - i)) { - System.out.println(format("%d+%d=%d", i, n-i, n)); - flag=1; + System.out.println(format("%d + %d = %d", i, n - i, n)); + flag = 1; } } else System.out.println("Wrong Input"); } -} \ No newline at end of file +} From 7892341682629bfa36463118b0814952f0eabb2f Mon Sep 17 00:00:00 2001 From: BILL_SARAN Date: Sat, 11 Jan 2025 20:33:18 +0200 Subject: [PATCH 3/3] Fixed formatting in GoldbachConjecture.java --- src/main/java/com/thealgorithms/maths/GoldbachConjecture.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java b/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java index 036ef2876235..1e07652930ee 100644 --- a/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java +++ b/src/main/java/com/thealgorithms/maths/GoldbachConjecture.java @@ -41,7 +41,7 @@ public static void main(String[] args) { int n = scanner.nextInt(); int flag = 0; - if(n%2 == 0 && n>2) { + if(n % 2 == 0 && n > 2) { for(int i = 0; i <= n/2 && flag == 0; i++) if(isPrime(i)) if(isPrime(n - i))