|
-
June 1st, 2008, 02:56 PM
#1
Not repeating an operation to cutdown runtime
Hi,
In my code, I have make an array a size dependent on the amount of numbers that meet two conditions and then fill that array with those numbers. The following code work, but I have to do the exact same operation twice, once to find the size of the array and the second to fill it. Is there a way to avoid this because if NUM is very big this process becomes very long (it's basically prime factorization).
Also, in the if statements, when the computer finds that the first one is false, does it bother checking the second or does it continue on?
Code:
class survivor
{
public:
int * pps;
int sizepps;
survivor()
{
sizepps = 0;
for(long long i = 3; i<NUM;i+=2)
{
if(checkPrime(i)&& NUM%i==0)
++sizepps;
}
pps = new int[sizepps]
int curloc=0;
for(long long i=3; i<NUM; i+=2)
{
if(checkPrime(i)&& NUM%i==0)
pps[curloc] = i;
}
}
};
Thanks for the help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|