Find out if a number is prime I read that every prime number is of the form $6k\pm1$, is this a correct approach to find out if a number is prime?
auto isPrime = [&](int num) {
      if (num == 0 || num == 1)
        return false;
      if (num == 2 || num == 3)
        return true;
      if ((num - 1) % 6 == 0 || (num + 1) % 6 == 0)
        return true;
      else
        return false;
    };

 A: No, and the informal argument is: that would be way too simple. Proving there is an efficient algorithm to check if a given number is prime was a big breakthrough in computational complexity, and only happened in 2002.
Your algorithm will accept $2,3$, and any number of the form $6k\pm 1$; but while every prime number is of this form, there are many numbers of this form that are not prime. E.g., $25$.
If you are looking for a deterministic algorithm (running in polynomial time) checking whether a given number is prime, I suggest you read about the AKS algorithm.
(Note that there are much more efficient randomized algorithms for doing so, e.g. Miller—Rabin; i.e., they will give the right answer with very high probability, but there is a slight chance they'll err.)
A: I can give you a easy trick
Step 1:
Check nearest perfect square number of given number
for example,Let given number is $131$.
So,nearest perfect square number is $144(12^2)$
Step 2: Find prime numbers $\lt 12$ i.e $2,3,5,7,11$
Step 3: Check divisibilty of $131$ by $2,3,5,7,11$
If it's not divisible by any of the number.Then it is prime.
So,$131$ is prime
