check_prime.cpp File Reference

Reduced all possibilities of a number which cannot be prime. Eg: No even number, except 2 can be a prime number, hence we will increment our loop with i+2 jumping on all odd numbers only. If number is <= 1 or if it is even except 2, break the loop and return false telling number is not prime. More...

`#include <cassert>`
`#include <iostream>`
Include dependency graph for check_prime.cpp:

## Functions

template<typename T >
bool | is_prime (T num)
int | main ()

## Detailed Description

Reduced all possibilities of a number which cannot be prime. Eg: No even number, except 2 can be a prime number, hence we will increment our loop with i+2 jumping on all odd numbers only. If number is <= 1 or if it is even except 2, break the loop and return false telling number is not prime.

Copyright 2020

Author
  omkarlanghe

A simple program to check if the given number if prime or not.

## Function Documentation

## is_prime()

template<typename T >

bool is_prime | ( | T | num | ) |

Function to check if the given number is prime or not.

Parameters

  num | number to be checked.

Returns
  if number is prime, it returns @ true, else it returns @ false.

{
bool result = true;
if (num <= 1) {
return 0;
} else if (num == 2) {
return 1;
} else if ((num & 1) == 0) {
return 0;
}
if (num >= 3) {
for (T i = 3; (i * i) < (num); i = (i + 2)) {
if ((num % i) == 0) {
result = false;
break;
}
}
}
return (result);
}
math::fibonacci_sum::result
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76

## main()

int main | ( | void |  | ) |

Main function

{
// perform self-test
assert(is_prime(50) == false);
assert(is_prime(115249) == true);
int num;
std::cout << "Enter the number to check if it is prime or not" << std::endl;
std::cin >> num;
bool result = is_prime(num);
if (result) {
std::cout << num << " is a prime number" << std::endl;
} else {
std::cout << num << " is not a prime number" << std::endl;
}
return 0;
}
std::cin
std::cout
is_prime
bool is_prime(T num)
Definition: check_prime.cpp:22
std::endl
T endl(T... args)
Here is the call graph for this function: