Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created October 22, 2012 10:44
Show Gist options
  • Save juanfal/3930889 to your computer and use it in GitHub Desktop.
Save juanfal/3930889 to your computer and use it in GitHub Desktop.
Averiguar por divisiones sucesivas si un número dado es o no primo
// primosimple.02.cpp
// juanfc 2011-10-11
// Averiguar por divisiones sucesivas
// si un número dado es o no primo
#include <iostream>
using namespace std;
bool esPrimo(unsigned n);
int main()
{
unsigned n;
cout << "Introducir n: " << endl;
cin >> n;
if (esPrimo(n))
cout << "SI es primo" << endl;
else
cout << "NO es primo" << endl;
return 0;
}
bool esPrimo(unsigned n)
{
unsigned i = 2;
while ( n % i != 0 )
++i;
return i >= n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment