loop calculation

So...i have to make a program that impruves the goldbich theory.
This theory says that every number can be the addition of two smaller numbers.
45:23+22. 12:4+8

And i stuck at this point! If there are a lot of additions i have to show only the one that has the smallest number of the firs. 4+8 3+9 2+10! It has to show the 2+10...

My code so far>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    int i,n,p,a;
    string line;
    
    system("color 2");
    ifstream f;
    f.open("Goldbach.in.txt");
    if(f.is_open());
    {
    getline (f,line);
    istringstream buffer(line);
    buffer>>a;
    2 < a <= 1000000;
    if(a<2)
    {
           cout<<"Invalid integer!!!"<<endl;
           }
    
    
    srand ( time(0) );
    p=rand() % a + 1;
    n=rand() % a + 1;
    p=a-n;
    
    


    cout<<p<<' '<<n<<endl;
    

    f.close();
}   
    
    system("PAUSE");
    
    return 0;
}

If anyone can help...please.
This theory says that every number can be the addition of two smaller numbers.
[...]i have to show only the one that has the smallest number of the firs
for integers output 1 and n-1
Do you mean Goldbach's conjecture? http://en.wikipedia.org/wiki/Goldbach%27s_conjecture because that's a REAL math problem. If not please link to Wikipedia or some other source so that we can better understand what we are trying to accomplish.

For now I'll chalk it up to not typing a clear description of the problem, if this is not the case then "Prove That Addition Exists" is the dumbest f---ing assignment I have ever heard.
Yes this is the theory. I have to pruve it! It s exactly like i made it... but i stuck at this point. I have to output the smallest number of the addition! for example....: P1+P2=N.
Should apply P1 + P2 = N. If there are many solutions it has to print the smaller P1.
The smallest P1 will ALWAYS BE 2 according to this conjecture.

P1 will always be 2 and P2 will always be N - P1.

Which is what I said in my first post to this thread, this is the dumbest assignment I have ever seen, I have no idea what they are trying to teach you about programming.
Last edited on
Ok. Thats true! But when i try after the rundom numbers to put [p<=3] it doesnt do anything.
It still get the rundom numbers and not the p<=3!
Where to put it???
@Computergeek01
I dont think so... it has very good logical thinking... P1 is not 2, it can be any Prime... i suggest rafailos that you do this on your own, so you will gain the best experience through the assignment.. i love things like this... try to do this on your own and feel the joy of it....

EDIT
--------------------------
Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathematics. It states:

Every even integer greater than 2 is a Goldbach number, a number that can be expressed as the sum of two primes.[1]

The number of ways an even number can be represented as the sum of two primes[2]

Expressing a given even number as a sum of two primes is called a Goldbach partition of the number. For example,

4 = 2 + 2
6 = 3 + 3
8 = 3 + 5
10 = 7 + 3 or 5 + 5
12 = 5 + 7
14 = 3 + 11 or 7 + 7


- Wikipedia
Last edited on
Fine with all these! But... i want a litle help...any ideas...! I cant even think about how to make it.
I am a beginer so i dont now a lot of things.
Store in an array all the relevant primes.
Traverse the array.
If the difference is a prime number too, that will the pair to output
Last edited on
i guess thats a bad idea.. how do you define "relevant primes".. you will need all the primes.. infinite number of primes... but in the other way you are correct that you store the smallest primes in an array ( i prefer using unsigned short integers for saving space and time ), so that you dont have to calculate the same old primes ( 2, 3, 5 , 7.... until about 101 )..

http://en.wikipedia.org/wiki/List_of_prime_numbers
this link will give you the first 500 primes i guess.. i recommend that you dont waste your time finding the rest because u will get sic of typing numbers, and any typing mistake you make will take you to hell...

this is a simple idea... you can use this or enhance this....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while(1){
Number - APrimeSmallerThanNumber == X;
if ( X is a prime )
{
 /* you win */
 }
else if ( there are more primes which are smaller that Number )
{
      /* try next prime which is smaller than array */
}
else
{
     /* something wrong with goldbatch or me..!!! :P */
}
}


the simple words as cooments and expressions such as "X is a prime", " try next prime" will be real pain to implement...
i wish you all the best...
how do you define "relevant primes".. you will need all the primes.. infinite number of primes...
If you want to test the golbach conjeture to numbers up to 1000000 you don't need primes greater that 1000000. In fact you only need the primes up to sqrt(1000000) = 1000. Those are the "relevant primes".

this link will give you the first 500 primes i guess.. i recommend that you dont waste your time finding the rest because u will get sic of typing numbers, and any typing mistake you make will take you to hell...
Then don't type them, but compute them instead.
I use the definition
_A number is prime if it cant be divided by any prime number less than or equal to its square root.
_2 is a prime number.

Because of memory ussage, instead of erasing the numbers that are not primes, insert the numbers that are.
So I start wiht a vector that has only the 2. Then test for modulo for every odd number against the numbers in the vector.
If it is a prime number, added to the vector and continue.

Now implementing X is prime, will be a search in an sorted array or use the definition again.
And try next prime will be prime_index++
yes ne555,
thats all what i meant, and also what i say is that you can type the little primes to an array just like you said earlier, so you would be saving some time cost of processer for finding those primes, and when you want to find the other primes ( greater than those in the array) you can calculate according to the definition of the primes..

i said that you will need infinite.. when you dont know the largest number you get to find.. but of course, if you are using unsigned short for example, you know that the largest number is 65536, and you will need not need those primes bigger than that...

cheers..
Topic archived. No new replies allowed.