// Programm: prim.C
// Testet, ob eine Zahl Primzahl ist.
// Wenn nicht, Ausgabe des kleinsten Primteilers.
#include
#include
#include
int main ()
{
// Einlesen der Eingabe
int zahl; // Eingabezahl
std::cout << "Zahl zwischen 2 und "
<< std::numeric_limits::max() << " ? ";
std::cin >> zahl;
if (zahl < 2) {
std::cout << "Unzulaessige Eingabe." << std::endl;
return 1;
}
// Test, ob die Eingabezahl einen nicht trivialen Teiler hat
int teiler = 1; // Potentielle Teiler der Eingabezahl
do {
teiler = teiler + 1;
// Invarianten: 1) 1 < teiler <= zahl
// 2) zahl == (zahl/teiler)*teiler + zahl % teiler
assert( teiler > 1 &&
teiler <= zahl &&
zahl == (zahl/teiler)*teiler + zahl % teiler);
} while (zahl % teiler != 0);
// Ausgabe des Ergebnisses
if (teiler == zahl)
std::cout << zahl << " ist Primzahl." << std::endl;
else
std::cout << zahl << " ist keine Primzahl und " << teiler
<< " ist der kleinste Primteiler von " << zahl
<< "." << std::endl;
return 0;
}