Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include "PrimalityChecker.decl.h"
- struct Number
- {
- long Value;
- bool IsPrime;
- };
- class Main : public CBase_Main
- {
- private:
- int count;
- int k;
- Number *numbers;
- public:
- Main(CkArgMsg *msg)
- {
- // To avoid generating the same random numbers.
- srand(time(0));
- if (msg->argc < 1)
- {
- ckout<<"Expecting one argument.";
- CkAbort("");
- }
- k = atoi(msg->argv[1]);
- count = k;
- numbers = new Number[k];
- for (int i=0; i<k; i++)
- {
- numbers[i].Value = rand();
- CProxy_CheckPrimality::ckNew(thisProxy, numbers[i].Value, i);
- }
- }
- void Done(int index, bool result)
- {
- count--;
- numbers[index].IsPrime = result;
- if (count == 0)
- {
- for (int i=0; i<k; i++)
- {
- ckout<<i+1<<"; Number: "<<numbers[i].Value<<"; IsPrime: "<<numbers[i].IsPrime<<endl;
- }
- CkExit();
- }
- }
- };
- class CheckPrimality : public CBase_CheckPrimality
- {
- private:
- const long number;
- bool isPrime()
- {
- // If the number is smaller than 1 or is even then it cannot be a prime.
- if (number <= 1 || number&1 == 0)
- {
- return false;
- }
- // Checking only till square root of the number since one of the factor
- // will be either less than or equal to the sqaure root.
- for (int i=2; i*i<=number; i++)
- {
- if (number%i == 0)
- {
- return false;
- }
- }
- return true;
- }
- public:
- CheckPrimality(CProxy_Main mainProxy, const long _number, const int index) : number(_number)
- {
- bool isPrimeResult = isPrime();
- mainProxy.Done(index, isPrimeResult);
- }
- };
- #include "PrimalityChecker.def.h"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement