Given an odd number N, the task is to find if the number can be represented as the sum of 3 prime numbers.
Examples:
Input: N = 7 Output: Yes Explanation: 2 + 2 + 3 = 7 Input: N = 17 Output: Yes Explanation: 2 + 2 + 13 = 17
Approach:
In number theory, Goldbach’s weak conjecture, also known as the odd Goldbach conjecture, the ternary Goldbach problem, or the 3-primes problem, states that Every odd number greater than 5 can be expressed as the sum of three primes. (A prime may be used more than once in the same sum.).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// if a number can // be represent as // as a sum of 3 primevoid check(int n) { if (n % 2 == 1 && n > 5) cout << "Yes\n"; else cout << "No\n";} // Driver codeint main(){ int a = 3; int b = 7; check(a); check(b); return 0;}// This code is contributed by 29AjayKumar |
Java
class GFG { // Function to check // if a number can // be represent as // as a sum of 3 prime static void check(int n) { if (n % 2 == 1 && n > 5) { System.out.println("YES"); } else { System.out.println("NO"); } } // Driver code public static void main(String[] args) { int a = 3; int b = 7; check(a); check(b); }} // This code is contributed by PrinciRaj1992 |
Python3
# Function to check # if a number can # be represent as # as a sum of 3 primedef check(n): if n % 2 == 1 and n > 5: print('YES') else: print('NO')# Driver codedef main(): a = 3 b = 7 check(a) check(b)main() |
C#
using System;class GFG { // Function to check // if a number can // be represent as // as a sum of 3 prime static void check(int n) { if (n % 2 == 1 && n > 5) { Console.Write("YES"); } else { Console.WriteLine("NO"); } } // Driver code public static void Main(String[] args) { int a = 3; int b = 7; check(a); check(b); }} // This code is contributed by PrinciRaj1992 |
Javascript
// Function to check // if a number can // be represent as // as a sum of 3 prime function check(n) { if (n % 2 == 1 && n > 5) { document.write("YES"); } else { document.write("NO" + "<br>"); } } // Driver code var a = 3; var b = 7; check(a); check(b); // This code is contributed by shivanisinghss2110 |
NO YES
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
