Trouble with a prime-summing program

This is a discussion on Trouble with a prime-summing program within the C++ Programming forums, part of the General Programming Boards category; Hi, I've tried to write a code to answer the question: "Which prime, below one-million, can be written as the ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    7

    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;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by C++ student View Post

    - 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 View Post
    - 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.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by C++ student View Post
    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.
    - 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?
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    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;
    			}
    		}

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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).
    Last edited by anon; 10-02-2009 at 12:55 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by C++ student View Post
    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.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. prime factorisation program
    By szpengchao in forum C Programming
    Replies: 4
    Last Post: 04-07-2009, 04:30 PM
  3. C Program that counts total prime number?
    By exe101 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 01:18 AM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  5. trouble with my first c program
    By mblazar in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 01:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21