Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created October 11, 2012 06:38
Show Gist options
  • Save juanfal/3870612 to your computer and use it in GitHub Desktop.
Save juanfal/3870612 to your computer and use it in GitHub Desktop.
simplest way of computing primality. Know if a number is prime. Slowest way
// 02.esprimo_simple.cpp
// juanfc 2009-10-16
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter an integer: ";
int n;
cin >> n;
int i = 2;
while (i < n and n % i != 0) // is necessary i<n?
++i;
if (i >= n)
cout << "YES, it is prime" << endl;
else
cout << "NO, it is not a prime" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment