Created
October 22, 2012 10:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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