Ad
  • Fixed function argument (should be signed)
  • Check only uneven numbers in the main loop and treat mod 2 as a special case.
  • Removed useless cast.
Code
Diff
  • class PrimeNumber {
      public:
        // Check if given number is prime or not.
        static bool isPrime(long long num) {
            if (num <= 1) return false;
            if (num == 2) return true;
            if (num % 2 == 0) return false;
            for (long long i = 3; i * i <= num; i += 2) 
                if (num % i == 0) return false;
            return true;
        }
    };
    • class PrimeNumber {
    • public:
    • // Check if given number is prime or not.
    • static bool isPrime(unsigned long long num ) {
    • if(num <= 1) return false;
    • for( long long i = 2; (long long) i * i <= num; i++)
    • if( num % i == 0) return false;
    • static bool isPrime(long long num) {
    • if (num <= 1) return false;
    • if (num == 2) return true;
    • if (num % 2 == 0) return false;
    • for (long long i = 3; i * i <= num; i += 2)
    • if (num % i == 0) return false;
    • return true;
    • }
    • };