Thread: Finding primes...

  1. #1
    ER
    Guest

    Unhappy Finding primes...

    Code:
    void determinePrimes(int maxPrime)
    {
    	int numPrimes = 0, *primes = new int[1.01 * (maxPrime / (log(maxPrime) - 1))];
    
    	for (int j = 3; j <= maxPrime; j += 2)
    		if (isPrime(j, primes))
    			primes[++numPrimes] = j;
    
    	delete[] primes;
    }
    
    inline bool isPrime(int number, int *primes)
    {
    	int biggestTest = sqrt(number) + 1;
    
    	for (int j = 0; primes[j] <= biggestTest; ++j)
    		if (number % primes[j] == 0) return 0;
    	return 1;
    }
    When I put in a number greater than around 100,000, the program performs an illegal operation and gets shut down. Is there any way I can allocate more memory for the array that seems to be causing the problem?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You sure it's not just overflowing the number of primes you allocated?

    I don't know about the mathematical frequency of primes in a list of in a general continous sequence of N integers (I've actually thought about it a bit though).

    It's possible, however, that there are more primes than

    1.01 * (maxPrime / (log(maxPrime) - 1)).

    Try making that 1.01 a 1.5 and seeing if that works.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    ER
    Guest
    Nope, that's not it...I tested it with a mathematical reference book. My algorithim allocates 78800 spaces in the array for 78498 primes, which is enough efficiency for me.

    What else could be the problem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Finding primes
    By scwizzo in forum C++ Programming
    Replies: 11
    Last Post: 09-10-2008, 06:15 PM
  3. primes program need some help
    By Mshock in forum C Programming
    Replies: 2
    Last Post: 04-17-2006, 07:21 PM
  4. Finding primes
    By starripper in forum C++ Programming
    Replies: 19
    Last Post: 01-14-2006, 04:17 PM
  5. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM