Code you presented is makeing a operations for every a. It is simple way to improve it:
boolean checkPrime(long a)
{
long count = 0L;
for(long op = 2;op*op<=a;op++)
if(a%op==0)
return false;
return true;
}
Now it makes only sqrt(a) operations and always gives right answer. For beter execution times are randomised algorithms like Rabin-Milers algorithm: http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
I include mine implementation in c++ of Rabin-Milers algorithm.
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
const int losowania=20;
long long pote(long long x,long long k,long long m)//this function is counting (x^k)mod m
{
if(k==1)
{
return x%m;
}
if(k%2==0)
{
long long a=pote(x,k/2,m);
return ((__int128_t)((__int128_t)a*(__int128_t)a)%(__int128_t)m);
}
else
{
long long a=pote(x,k-1,m);
return ((__int128_t)((__int128_t)a*(__int128_t)x)%(__int128_t)m);
}
}
bool Rabin_Miler(long long p,long long x)
{
if(pote(x,p-1,p)!=1)
{
return false;
}
long long wyk=(p-1);
while(wyk%2==0)
{
wyk/=2;
}
long long teraz=pote(x,wyk,p);
if(teraz==1)
{
return true;
}
while(teraz!=1&&teraz!=p-1)
{
teraz=(__int128_t)((__int128_t) teraz*(__int128_t)teraz)%(__int128_t)p;
}
if(teraz==1)
{
return false;
}
else
{
return true;
}
}
bool is_prime(long long p)
{
srand(100);
for(int i=0;i<losowania;i++)
{
if(!Rabin_Miler(p,(rand()%(p-1))+1))
{
return false;
break;
}
}
return true;
}