<p>If your input is less than 2^64 (more than enough for your example using an int) there are some good methods:</p>

<p>1) <a href="https://en.wikipedia.org/wiki/Baillie%E2%80%93PSW_primality_test" rel="nofollow">BPSW</a>.  Fast, deterministic, correct for all 64-bit inputs, no known counterexamples above this (though we believe they exist)</p>

<p>2) <a href="https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Deterministic_variants_of_the_test" rel="nofollow">Deterministic Miller-Rabin</a>.  The Wikipedia page gives some correct but inefficient base sets -- the ones at <a href="https://miller-rabin.appspot.com/" rel="nofollow">Best Known SPRP Bases</a> are the best known for 64-bit inputs.  At most 7 tests for any 64-bit input.  <a href="http://arxiv.org/abs/1509.00864" rel="nofollow">New (Sep 2015) results</a> give deterministic results for 81-bit input with 13 tests.</p>

<p>3) Hashed deterministic M-R.  This is just an optimization of #2.  Only a single M-R test needed for 32-bit inputs, 2 or 3 for 64-bit inputs.  See <a href="http://ceur-ws.org/Vol-1326/020-Forisek.pdf" rel="nofollow">Forisek and Jancina 2015 paper</a> and <a href="https://github.com/danaj/Math-Prime-Util/blob/master/primality.c" rel="nofollow">my different hashed implementation</a>.</p>

<p><a href="https://en.wikipedia.org/wiki/Trial_division" rel="nofollow">Trial division</a>, the method you're showing, is quite good for tiny inputs, say under a million or so.  It is still computationally ok for a while past that, but it is exponential time in the bit length of the input.  It slows down very rapidly, and really isn't usable past 26 or so digits (just because of the huge time growth).  In my test, at 25 digits it is 400M times slower than BPSW (a PRP test at this size), 13M times slower than ECPP, 3M times slower than APR-CL.</p>

<hr>

<p><a href="http://probableprime.org/images/primality-times.png" rel="nofollow">Graph of run times for primality tests on large inputs</a></p>

<hr>

<p>If your input is larger than 64-bit, some options include:</p>

<ul>
<li><p>BLS75 methods (from <a href="http://www.ams.org/journals/mcom/1975-29-130/S0025-5718-1975-0384673-1/" rel="nofollow">the seminal 1975 paper</a>), including N-1, N+1, and hybrid methods based on partial factoring.  These are still used, and are surprisingly fast for numbers up to ~40 digits.  <a href="https://en.wikipedia.org/wiki/Pocklington_primality_test" rel="nofollow">Generalized Pocklington</a> is a special case of one of the theorems.  Since this relies on partial factoring of n-1 and/or n+1, it doesn't scale well in general and fizzles out around 80-100 digits for practical use.</p></li>
<li><p><a href="https://en.wikipedia.org/wiki/Adleman%E2%80%93Pomerance%E2%80%93Rumely_primality_test" rel="nofollow">APR-CL</a>.  Quite fast (e.g. half a second for a 200 digit number).  Open source in <a href="http://pari.math.u-bordeaux.fr/" rel="nofollow">Pari/GP</a> and <a href="http://sourceforge.net/projects/mpzaprcl/" rel="nofollow">mpz_aprcl</a>.</p></li>
<li><p><a href="https://en.wikipedia.org/wiki/Elliptic_curve_primality" rel="nofollow">ECPP</a>.  Fastest method for large inputs not of special form.  <a href="http://www.ellipsa.eu/public/primo/primo.html" rel="nofollow">Primo</a> (free to use and the gold standard), <a href="http://sti15.com/nt/ecpp-dj.tar.gz" rel="nofollow">ecpp-dj</a> (open source).  This uses randomization, so it isn't deterministic in some sense, but it is 100% correct, which is what many people mean in this context.  It also can provide a certificate for fast third-party validation, making it especially attractive.</p></li>
<li><p><a href="https://en.wikipedia.org/wiki/AKS_primality_test" rel="nofollow">AKS</a>.  <em>Horrendously</em> slow.  Theoretical breakthrough and fascinatingly simple math, but practically useless.  It is faster than trial division at 20 or so digits, and eventually will pass the BLS75 methods, but it's nowhere close to the methods we usually use: APR-CL or ECPP.  Various implementations exist, with the fastest I'm aware of being in <a href="http://sti15.com/nt/ecpp-dj.tar.gz" rel="nofollow">ecpp-dj</a> and <a href="https://github.com/danaj/Math-Prime-Util/blob/master/aks.c" rel="nofollow">Perl/ntheory</a> [caveat: I'm the author].  Polynomial time, but the exponent is higher than APR-CL for inputs under a quadrillion or so digits (ridiculously large sizes).</p></li>
</ul>
Is there any deterministic method to check whether a number is prime or not?
<p>And also is this method a deterministic method, written below:</p>

<pre><code>bool isPrime(int a){
    if( a &lt;= 0) return false;
    if( a == 1) return false;
    if( a == 2) return true;
    if( a == 3) return true;
    int sqr = sqrt(a)+1;
    if( a%2 == 0) return false;
    for(int i=3;i&lt;=sqr;i+=2){
        if( a%i == 0 )
           return false;
    }
    return true;
}
</code></pre>