DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   need help with prime (http://www.daniweb.com/forums/thread164805.html)

firstPerson Dec 29th, 2008 3:10 pm
need help with prime
 
hi,

I was trying to implement the sieve of eroathoses (I know I spelt this wrong) and it works. But i was trying to find all prime under 2 million.

The .cmd for visual studio just corrupts if I try to find all prime under 2million. I am guessing It's because overflow or something but I don't know for sure.

can you give me some hints on how to fix my code so the program won't corrupt. here is the code:

#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>

using namespace std;

void sieve(__int64 arry[], __int64 upto) // finds prime num.
{       
       

        int next(0);

        for(__int64 i=0; i < upto; i++)//populate the arry starting from the value 2.
                        arry[i] = i+2 ;
       
       

        for(__int64 k=1; k < upto;k++)
        {
                if(arry[k] >=1)
                {
                        next = arry[k];

                        for(int l = k+1 ; l <= upto ; l++)

                                if(arry[l] % next==0)
                                        arry[l]=0;
                }
        }
               
for(__int64 i=0;i<=upto;i++)    //cout<< arry[] if it has a number greater than 0
       
        {
                if(arry[i]>0)
                        cout<<arry[i]<<endl;
        }       
       
                       
}

       



int main()
{
__int64 upto =200;  //This works but 2 million does not.
__int64 arry[201];    //same problem as above.

sieve(arry,upto);
 return 0;
}

thanks

cikara21 Dec 29th, 2008 3:24 pm
Re: need help with prime
 
Yeay...maks array size is 65535 or 65536...i dont know exactly...

firstPerson Dec 29th, 2008 3:26 pm
Re: need help with prime
 
what???

cikara21 Dec 29th, 2008 3:30 pm
Re: need help with prime
 
ur using 32 bit compiler rit...

see this...

__int64 arry[201];

// in 32 bit compiler will be
int arry[201];

// maks array size = 65535

__int64 arry[2000000];

// ?????

firstPerson Dec 29th, 2008 3:38 pm
Re: need help with prime
 
Quote:

Originally Posted by cikara21 (Post 766686)
what do u mean...ur using 32 bit compiler rit...

I mean that when i try to find the all prime numbers below 2 million,
the program corrupts, but if i try to find all prime number below say 2000, it works perfectly. Try the program to see what i mean.

I don't get why it does not work for a large number.

Freaky_Chris Dec 29th, 2008 3:48 pm
Re: need help with prime
 
Static memory space is generally very limited, if you want to create large array sizes then you need to use dynamic memory.
__int64 *array = new __int64[2000000];
But be sure to delete the array afterwards
delete []array;
array = NULL; // to make sure you do not use the pointer

Chris

firstPerson Dec 29th, 2008 11:12 pm
Re: need help with prime
 
Quote:

Originally Posted by Freaky_Chris (Post 766696)
Static memory space is generally very limited, if you want to create large array sizes then you need to use dynamic memory.
__int64 *array = new __int64[2000000];
But be sure to delete the array afterwards
delete []array;
array = NULL; // to make sure you do not use the pointer

Chris

hmm. not sure what you mean. I know the basics about pointers but haven't learned what new is or does. Care to explain?

da penguin Dec 30th, 2008 9:43 am
Re: need help with prime
 
new allocates dynamic memory (did i choose the right words?).

firstPerson Dec 30th, 2008 2:14 pm
Re: need help with prime
 
So it's like i'm telling the computer to make extra memory by saying

__int64 *array = new __int64[2000000];

Is it the 'new' that tells the computer to make extra room?

Freaky_Chris Dec 30th, 2008 5:10 pm
Re: need help with prime
 
When you create a pointer to a type you reserve enough space in memory for the the variable. When you call new you actually allocate the memory to the program so it cannot be used by another program.

Chris


All times are GMT -4. The time now is 5:42 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC