# Trouble with a prime-summing program

• 10-01-2009
C++ student
Trouble with a prime-summing program
Hi, I've tried to write a code to answer the question:
"Which prime, below one-million, can be written as the sum of the most consecutive primes?"

But I've run into a few problems... (code is below)

- In compiling it says that "PrimeArray" and "iii" aren't declared in the "SumDown()" function. However, I already declared them in main(), so shouldn't that declare them in the function?

- Also, when I say:

Code:

``` int PrimeArray[50000]; int iii = 1; PrimeArray[iii] = nNumber;```
Apparently when I debugged it, it stayed as PrimeArray[iii], and did not become PrimeArray[1], how can I make this happen?

Here is the full code, by the way...

Thanks!

Code:

```/* Question: Which prime, below one-million, can be written as the sum of the most consecutive primes? Plan: 1. Define an array with 50000 elements   (a reasonable number considering there are about 78498 primes below one million) 2. Run a loop to test for Primes up to 50000. 3. Place each prime sequentially in the array. 4. After finding each new prime, add downwards in the array,   noting at each stage any new primes formed. 5. From this, take the record the largest sum that forms a prime in the downwards addition. 6. Continue this process, stopping when the largest prime exceeds 1,000,000. 7. Print the prime. */ #include "stdafx.h" #include <iostream> #include <cmath> using namespace std; bool isPrime(int nNumber) //this is a simple prime test, nothing too fancy {                        int nDivisorCount = 0;                 for(int nDivisor = 2; nDivisor <= static_cast<int>(sqrt(static_cast<double>(nNumber))); nDivisor++)                 {                         if(nNumber % nDivisor == 0)                         {                                 nDivisorCount += 1;                                 break;                         }                 }                 if(nDivisorCount == 0)                 return true;                 else                 return false; } int SumDown(int x) //sums down in the array from highest prime to lowest {         int nPrimeSum = 0;         int nTermsInSum = 0;         int nMaxTermsInThisSum = 0;         for(int jjj = iii; jjj > 0; jjj--)         {                 nTermsInSum += 1;                 nPrimeSum += PrimeArray[jjj];                 if (nPrimeSum >= 50000)                         break;                 if(isPrime(nPrimeSum) && (nTermsInSum > nMaxTermsInThisSum))                 {                         nMaxTermsInThisSum = nTermsInSum;                 }         }         return nMaxTermsInThisSum; } int main() {         int PrimeArray[50000];         PrimeArray[0] = 2; //take care of 2 since it's tricky         int nMaxTermsInAnySum = 0;         int nPrimeWithLongestSum;         int iii = 1; //denotes array slot into which primes are placed. Start at 1 because '2' has already been placed.         for(int nNumber = 3; nNumber <= 50000; nNumber++)         {                 if(isPrime(nNumber))                 {                         PrimeArray[iii] = nNumber;                         iii += 1;                         if(SumDown(iii) > nMaxTermsInAnySum)                         {                                 nMaxTermsInAnySum = SumDown(iii);                                 nPrimeWithLongestSum = nNumber;                         }                 }         }         cout << "The Prime " << nPrimeWithLongestSum << " can be expressed as the sum of " << nMaxTermsInAnySum << " primes." << endl;         return 0; }```
• 10-01-2009
tabstop
Quote:

Originally Posted by C++ student

- In compiling it says that "PrimeArray" and "iii" aren't declared in the "SumDown()" function. However, I already declared them in main(), so shouldn't that declare them in the function?

HA HA HA HA!

Quote:

Originally Posted by C++ student
- Also, when I say:

Code:

``` int PrimeArray[50000]; int iii = 1; PrimeArray[iii] = nNumber;```
Apparently when I debugged it, it stayed as PrimeArray[iii], and did not become PrimeArray[1], how can I make this happen?

I have no idea what you think you mean here.
• 10-01-2009
abachler
Quote:

Originally Posted by C++ student
Hi, I've tried to write a code to answer the question:
"Which prime, below one-million, can be written as the sum of the most consecutive primes?"

But I've run into a few problems... (code is below)

- In compiling it says that "PrimeArray" and "iii" aren't declared in the "SumDown()" function. However, I already declared them in main(), so shouldn't that declare them in the function?

I just spit my milk on my montitor, lol. No, local variables by definition only have local scope. So a variable declared in main is only visible to code in main.
Quote:

- Also, when I say:

Code:

``` int PrimeArray[50000]; int iii = 1; PrimeArray[iii] = nNumber;```
Apparently when I debugged it, it stayed as PrimeArray[iii], and did not become PrimeArray[1], how can I make this happen?

Here is the full code, by the way...

Thanks!
Yeah that doesn't make any sense, your explanation that is, what were you expecting, for it to change the code?
• 10-02-2009
C++ student
lol thanks for the help
for the [iii] what I was expecting was that since iii started at 1, PrimeArray[iii] would initially be equivalent to PrimeArray[1], and as 'iii' increases, it would be equivalent to PrimeArray[2], PrimeArray[3] etc.
However, when I say PrimeArray[iii] it doesn't seem to be taking on the 'value' that iii has

Code:

```int main() {         int PrimeArray[50000];         PrimeArray[0] = 2; //take care of 2 since it's tricky         int nMaxTermsInAnySum = 0;         int nPrimeWithLongestSum;         int iii = 1; //denotes array slot into which primes are placed. Start at 1 because '2' has already been placed.         for(int nNumber = 3; nNumber <= 50000; nNumber++)         {                 if(isPrime(nNumber))                 {                         PrimeArray[iii] = nNumber;                         iii += 1;                         if(SumDown(iii) > nMaxTermsInAnySum)                         {                                 nMaxTermsInAnySum = SumDown(iii);                                 nPrimeWithLongestSum = nNumber;                         }                 }```
• 10-02-2009
anon
Code that compiles would be nice. For example, do you have iii declared both at global and local scope?

Edit: SumDown tries to use the variable iii, but in fact the value of iii is passed to an unused parameter x. And you might need to declare PrimeArray as a global (or you could pass it to SumDown).
• 10-02-2009
tabstop
Quote:

Originally Posted by C++ student
lol thanks for the help
for the [iii] what I was expecting was that since iii started at 1, PrimeArray[iii] would initially be equivalent to PrimeArray[1], and as 'iii' increases, it would be equivalent to PrimeArray[2], PrimeArray[3] etc.
However, when I say PrimeArray[iii] it doesn't seem to be taking on the 'value' that iii has

Except, of course, it does. Why do you think it does not? (Of course, you should note that you are adding 1 to iii before you pass it on to the next function, so that's probably an error.)