Skip to content

Instantly share code, notes, and snippets.

@Brli
Last active November 8, 2015 05:04
Show Gist options
  • Save Brli/c69f54f7754ad2a1995e to your computer and use it in GitHub Desktop.
Save Brli/c69f54f7754ad2a1995e to your computer and use it in GitHub Desktop.
test if a int is prime
#include <iostream>
#include <cmath>
bool isPrime(int n) {
int m = (int)std::sqrt(n);
if (m != 2)
while (m > 2){
if(n % m == 0)
return false;
else
m--;
}
else if (n % m == 0)
return false;
else
return true;
}
int main(int argc, char **argv) {
int n;
std::cout << "Please enter a number: " << std::endl;
std::cin >> n;
if (isPrime(n) == true)
std::cout << "The number is prime." << std::endl;
else
std::cout << "The number is divisable!" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment