Department of Computer Science | Institute of Theoretical Computer Science | CADMO

Theory of Combinatorial Algorithms

Prof. Emo Welzl and Prof. Bernd Gärtner

// Program: prime.cpp // Test if a given natural number is prime. #include int main () { // Input unsigned int n; std::cout << "Test if n>1 is prime for n =? "; std::cin >> n; // Computation: test possible divisors d unsigned int d; for (d = 2; n % d != 0; ++d); // Output if (d < n) // d is a divisor of n in {2,...,n-1} std::cout << n << " = " << d << " * " << n / d << ".\n"; else // no proper divisor found std::cout << n << " is prime.\n"; return 0; }