Click here to Skip to main content
14,933,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Waring’s prime number conjecture (named after English mathematician Edward Waring) is a conjecture which states that any odd positive integer greater than five (5) can be expressed as the sum of three prime integers [2]. Take for example
7 = 3 + 2 + 2

My code:
C++
#include <iostream>
using namespace std;

bool isOdd(int x);
bool isPrime(int x);
void printWaringTrio(int x);

int main()
{
    printWaringTrio(7);
    return 0;
}

bool isOdd(int x){
    if (x % 2 != 0){
        return true;
    } else {
        return false;
    }
}

bool isPrime(int x){
    bool result = true;
    if (x == 0 || x == 1){
        result = false;
    } else {
        for (int i = 2; i <= x/2; ++i){
            if (x % i == 0){
                result = false;
                break;
            }
        }
    }
    if (result){
        return result;
    } else {
        return false;
    }
}

void printWaringTrio(int x){
    int sum = 0;
    if (x > 5 && isOdd(x) == 1){
        cout << x << " = ";
        for (int i = 1; i <= 3; i++){
            //I'm stuck in here!!! Pls help!
        }
    }
}


What I have tried:

I got stuck in:
void printWaringTrio(int x)
in displaying the trio.
Posted
Updated 10-Jun-21 13:43pm
v4

Quote:
I have written all the codes except for the part in:

Which translate to: "I've done nothing that is specific to the task I have been assigned."

Analysing the problem and working out how to solve it is the main part of the task: the actual coding is the trivial bit. This is a "learning exercise homework" intended to get you thinking about how to solve problems, rather than how to code.

So start by thinking "how would I do this by hand on paper?" and work that out first - from there, it should be fairly simple to design an app to do it.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
   
Comments
CPallini 10-Jun-21 7:53am
   
5.
Quote:
for (int i = 1; i <= 3; i++){
//I'm stuck in here!!! Pls help!
}
There, you should try to find the three prime numbers, say a, b, c.

You might, at first, try a brutal force approach:

  • Compute a vector, say v of prime numbers less than x.
  • Write two nested loops, each of them running on all v items.
  • At each iteration, the outer loop gives you a, while the inner loop gives you b.
  • Then compute c = x - a - b;. If c is in v then you are a lucky man. Otherwise continue iterating.


Once you implemented it, you may consider some optimizations.
   
Comments
Greg Utas 10-Jun-21 7:42am
   
5.
CPallini 10-Jun-21 7:52am
   
Thank you.
KarstenK 10-Jun-21 10:13am
   
int main() {
/// TODO
}

plz gimme code :-)))
You're breaking up the problem into smaller functions, which is good.

Although it can be taken too far, tighter code is usually preferable. For example:
C++
bool IsOdd(int x)
{
   return (x % 2 != 0);
}

bool IsPrime(int x)
{
   if(x <= 1) return false;  // return immediately if x is "invalid"
   ...
}
Maybe someone told you that a function should only return from one place. That's nonsense and can lead to verbose code.

To determine if x is prime, do you have to try divisors up to x/2, or can you stop earlier?

As for where you're stuck, printWaringTrio(x) needs access to all primes less than x. If you build that list, how can you search all combinations of those primes to find one that sums to x?
   
Comments
CPallini 10-Jun-21 7:53am
   
5.
Greg Utas 10-Jun-21 8:37am
   
Grazie.
Greg Utas 10-Jun-21 8:36am
   
.
Quote:
How to code and display a waring’s prime number conjecture in C++?

In short, you have done nothing related to the Waring prime number conjecture, the problem is that it is the object of this homework.
Advice: Take a sheet of paper and a pencil and train yourself to find solutions for different input integers. How you find solutions is basically your algorithm.
Since the 3 integers that make a solution are linked, a solution is a triplet.
C++
void printWaringTrio(int x){
    int sum = 0;
    // you may have to set an array to hold the 3 integers of solution
    if (x > 5 && isOdd(x) == 1){
        cout << x << " = ";
        // find the triplet of integers that are a solution for x
        for (int i = 1; i <= 3; i++){
            // print each integers of solution
        }
    }
}

Since you will continuously check if an integer is a prime, it may be faster to build a table of primes to avoid rechecking same integers many times.

Advice: Learn one or more analyze methods, E.W. Djikstra/N. Wirth Stepwize Refinement/top-Down method is a good start.
Structured Programming.pdf[^]
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
Program Development by Stepwise Refinement[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900