• ### Popular Now

• 15
• 15
• 11
• 9
• 10

#### Archived

This topic is now archived and is closed to further replies.

This topic is 5116 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

#include"mThread.h"
#include<fstream.h>
#include<limits.h>
void main()
{
unsigned int prgStart = GetTickCount();

ofstream file;
file.open("primesCount.txt",ios::out);

for(int q = 1;q < 32;q++)
{
switch(q)
{
}
}
for(q = 0; q < 32;q++)
q--;
for(q;q>=0;q--)

file.close();
cout.ios::precision(10);
cout<<"process complete in "<<double((GetTickCount() - prgStart)/1000.0)<<" seconds" <<endl;

}

#pragma once
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>

{
protected:
bool    d_bIsRunning;

public:

void Begin();
void End();
void Sus()
{
}
void Res()
{
}
bool IsRunning();

};

#pragma once

#include<fstream.h>
#include<math.h>

{
public:
inline bool isPrime();
inline void report()
{
numPrimes++;
fout.open(fileName,ios::app);
fout<<n<<endl;;
fout.close();
check_limit();
}
inline void request_kill()
{
}
inline void check_limit()
{
request_kill();
}
int numPrimes;
ofstream fout;
char *fileName;
bool myPrime;
unsigned long n;
};

#pragma once
#endif
{
n = 1;
}
{
unsigned long sqr = sqrt(n);
for (unsigned long x = 3; (x  <= sqr); x +=2)
if(n % x == 0)
return false;
return true;
}
{
End();
}
{
unsigned int startTime = GetTickCount();
RESET:
/*
{

Sus();
}
Sleep(1);*/
{
n+=2;
break;
if(n % 2 == 1)
myPrime = this->isPrime();
else
myPrime = false;
if(myPrime)
else
{
}
break;

report();
break;
End();
break;
}
goto RESET;
return 0;
}

it finds too many primes in the test set. (ie 84 are found in 1 - 320). im not sure what i do wrong though can any one help?

##### Share on other sites
Dood you''re code is WAY longer than it should be.
Try using things like:

to get rid of the switch statement.
Try to get rid of your thread[0].xxxx statements as you should be able to make it the same for every case. Make it part of the loop.

This part has a bug in it:
q--;
for(q;q>=0;q--)
The first iteration starts at 31. I assume you don''t mean to add item 31 to item 31 to begin with?! I suggest using a temporary instead of one of the array items. Also it''s bad practice to start the for last for loop above the way you do. make it say q=31 so that it is obvious.

What gives your threads time to do anything between the Begin line and the adding up of the numPrimes? Something is missing there.

Your isPrime function should start at 2, not 3 and must step by 1 not 2. That is another reason why you have too many found.

I hope this doesn''t sound too flamey. Its just that I had to fix code like this I''d start over from scratch.

think ... plan ... code

##### Share on other sites
well, the printf thing doesnt work, i knew about the for loop bug and have since fixed it, though i avoided it cus i knew it was only off by threads[31].numPrimes which only accounts for 1 when testing 320 numbers, and thus irrelevant.

the isPrime function for one is never passed even numbers and i dont need to worry about 2 being prime i know its prime, and for two starting it at 3 and adding 2 means im only testing odd numbers since all even numbers dont need to be tested at all.

and as for the lettign the code work after the Begin to before the adding of numPrimes, it seems to work but just in case what do you suggest i do to allow that ?

##### Share on other sites
fstream.h is deprecated, use fstream (no .h) and the std namespace.

You shouldn''t start programming (in C++ or otherwise) with multi-threaded code. Write a single-threaded version that works flirst.