#include #include #define classdef typedef #include using namespace std; namespace { classdef vector CVUInt; typedef CVUInt::size_type TypeInd_t; void AffichVect (const CVUInt & Tab) { TypeInd_t Nb = 10; for (TypeInd_t i = 0; i < Tab.size(); ++i) { cout << setw (5) << Tab [i]; if (0 == --Nb) { cout << '\n'; Nb = 10; } } if (Nb > 0) cout << '\n'; } // AffichVect() void GenerPrimesInfEqN (CVUInt & LesPremiers, unsigned N) { LesPremiers.resize ((N + 1) / 2); // Initialiser le tableau avec les premiers entiers naturels impairs LesPremiers [0] = 2; unsigned NbElem = 1; unsigned i = 3; while (i <= N) { LesPremiers [NbElem] = i; ++NbElem; i += 2; } unsigned j; i = 1; while (LesPremiers [i] * LesPremiers [i] <= N) { // Supprimer les multiples de LesPremiersi if (LesPremiers [i] != 0) { j = i + LesPremiers [i]; while (j < NbElem) { LesPremiers [j] = 0; j += LesPremiers [i]; } } ++i; } j = 0; for (unsigned i =0; i < NbElem ; ++i) { if (LesPremiers [i] != 0) { LesPremiers [j] = LesPremiers [i]; ++j; } } LesPremiers.resize (j); } } //namespace int main (void) { CVUInt MonVector; unsigned N; cout << "Saisissez N : "; cin >> N; GenerPrimesInfEqN (MonVector, N); cout << "N saisi : " << N << endl; AffichVect (MonVector); return 0; }