• Операції над масивами1) З множини цілих чисел [1..1000] методом решета Ератосфена отримати множину простих чисел і вивести їх на екран.С++

Ответы 1

  • #include <iostream>

    #include <bitset>

    #include <math.h>

    const int N = 1000;

    void SieveOfEratosthenes(int n) {

     std::bitset<N + 1> is_prime;

     is_prime.set(); // set all bits to 1

     is_prime[0] = is_prime[1] = 0; // 0 and 1 are not prime

     for (int i = 2; i <= sqrt(n); ++i) {

       if (is_prime[i]) {

         // Mark all multiples of i as not prime

         for (int j = i * i; j <= n; j += i) {

           is_prime[j] = 0;

         }

       }

     }

     // Print all prime numbers

     for (int i = 2; i <= n; ++i) {

       if (is_prime[i]) {

         std::cout << i << " ";

       }

     }

    }

    int main() {

     SieveOfEratosthenes(N);

     return 0;

    }

  • Добавить свой ответ

Войти через Google

или

Забыли пароль?

У меня нет аккаунта, я хочу Зарегистрироваться

How much to ban the user?
1 hour 1 day 100 years