This function determines whether n is a prime number or not.

You're viewing an older version of this page (#628). View the current version.

View versions (2)

Interface

#include <codecogs/maths/discrete/number_theory/is_prime.h>

using namespace Maths::Discrete::Number_Theory;

This function looks for possible divisions, when one is found, it returns false, otherwise, it returns true.

Example 1

This example computes the first set of prime numbers:

#include <iostream>
#include <codecogs/maths/discrete/number_theory/is_prime.h>
int main(int argc, char **argv)
{
    for(unsigned int i = 2; i < UINT_MAX; i++)
    {
        if(isPrime(i))
            std::cout << i << std::endl;
    }
    return 0;
}

Output

2, 3, 5, 7, 11, 13, .......... until UINT_MAX!

Parameters

n
Input number

Returns

True or false
GPL Licence — free for non commercial use. See Licence details.

Interactive Calculator

n
Result