Search
 
SCRIPT & CODE EXAMPLE
 

CPP

check prime number c++

bool prime(int a) { 
    if (a < 2)
        return false;
    for (int i = 2; i * i <= a; i++) {
        if (a % i == 0)
            return false;
    }
    return true;
}
Comment

how to check if a number is prime c++

bool isPrime(int number){

    if(number < 2) return false;
    if(number == 2) return true;
    if(number % 2 == 0) return false;
    for(int i=3; (i*i)<=number; i+=2){
        if(number % i == 0 ) return false;
    }
    return true;

}
Comment

fast way to check if a number is prime C++

//O(sqrt(n))
bool isPrime(int num){
    if(num <= 1) return false;
    for(int i = 2; i <= sqrt(num); i++){
          if(num % i == 0) return false;
    }
    return true;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ reading string 
Cpp :: int to string c++ 
Cpp :: height of bst cpp 
Cpp :: convert binary string to int c++ 
Cpp :: How to reverse a string in c++ using reverse function 
Cpp :: c++ template example 
Cpp :: print each number of digit c++ 
Cpp :: c++ create multidimensional vector 
Cpp :: how to make a typing effect c++ 
Cpp :: how to do sets in cpp 
Cpp :: initialize 2d vector 
Cpp :: Parenthesis Checker using stack in c++ 
Cpp :: string in cpp 
Cpp :: c++ reference 
Cpp :: comparator for priority queue c++ 
Cpp :: size of stack in c++ 
Cpp :: c++ call by reference 
Cpp :: c++ string element access 
Cpp :: insert only unique values into vector 
Cpp :: hello world in c++ 
Cpp :: c++ initialize vector of vector with size 
Cpp :: prisma client 
Cpp :: inline in class in C++ 
Cpp :: how to write hello world in c++ 
Cpp :: cpp float 
Cpp :: c detect os 
Cpp :: c++ hello world linux 
Cpp :: input n space separated integers in c++ 
Cpp :: print stack without pop c++ 
Cpp :: best time to buy and sell stock leetcode solution 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =