I am currently making a program related to the Goldbach Conjecture, and needed some help in a particular section, the program calls this function quite often and the 'old' value is changed accordingly. I am able to get through 2 calls of this function successfully, but on the 3rd call, nothing happens.
1 2 3 4 5 6 7 8 9 10 11
void give_prime(int status, int &old)
{
if (status)
{
if (old==2) old++;
elsefor (;;old+=2)
for (int j=2;j<old;j++) if(old%j==0) break;
}
else old=2;
}
Could this be a compiler error or is there some defect in the program?
(nb - I have debugged and checked the other part of the code, and have pin pointed, that it is at this function call itself that the program does not proceed, that is, at the 3rd call.)
As soon as line 7 is executed you will have an infinite loop. The break on line 8 will only stop the inner loop, but the outer loop for (;;old+=2) will never stop.
Yeah, thanks, slipped by me. But, yet another problem popped up.
Firstly, the Golbach Conjecture states that every even number greater than or equal to 4 can be represented as the sum of two primes.
So now, it displays them appropriately, however, every multiple of 6 is incorrect, more precisely, the first number turns out to just be a multiple of 6.
for example -
4=2+2 //correct
6=3+3//correct
8=3+5//correct
And so on, however..
(included program output)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Enter the limit : 25
4 = 2 + 2
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7
12 = 9 + 3 //INCORRECT AS 9 NOT A PRIME
14 = 3 + 11
16 = 3 + 13
18 = 15 + 3 //INCORRECT
20 = 3 + 17
22 = 3 + 19
24 = 21 + 3 //INCORRECT
Process returned 0 (0x0) execution time : 2.644 s
Press any key to continue.