Printing prime numbers from 1 through 100

前端 未结 22 2482
无人共我
无人共我 2020-11-28 05:14

This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97.

But I don\'t think tha

22条回答
  •  隐瞒了意图╮
    2020-11-28 06:06

    I check if a number is prime or not with the following code( of course using sqrt ):

    bool IsPrime(const unsigned int x)
    {
      const unsigned int TOP
      = static_cast(
          std::sqrt( static_cast( x ) )
        ) + 1;
    
      for ( int i=2; i != TOP; ++i )
      {
        if (x % i == 0) return false;
      }
      return true;
    }
    

    I use this method to determine the primes:

    #include 
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include 
    
    void initialize( unsigned int *, const unsigned int );
    void show_list( const unsigned int *, const unsigned int );
    void criba( unsigned int *, const unsigned int );
    void setItem ( unsigned int *, const unsigned int, const unsigned int );
    
    bool IsPrime(const unsigned int x)
    {
      const unsigned int TOP
      = static_cast(
          std::sqrt( static_cast( x ) )
        ) + 1;
    
      for ( int i=2; i != TOP; ++i )
      {
        if (x % i == 0) return false;
      }
      return true;
    }
    
    int main()
    {
    
        unsigned int *l;
        unsigned int n;
    
        cout << "Ingrese tope de criba" << endl;
        cin >> n;
    
        l = new unsigned int[n];
    
        initialize( l, n );
    
        cout << "Esta es la lista" << endl;
        show_list( l, n );
    
        criba( l, n );  
    
        cout << "Estos son los primos" << endl;
        show_list( l, n );
    }
    
    void initialize( unsigned int *l, const unsigned int n)
    {
        for( int i = 0; i < n - 1; i++ )
            *( l + i ) = i + 2;
    }
    
    void show_list( const unsigned int *l, const unsigned int n)
    {
        for( int i = 0; i < n - 1; i++ )
        {
            if( *( l + i ) != 0)
                cout << l[i] << " - ";
        }
        cout << endl;
    }
    
    void setItem( unsigned int *l, const unsigned int n, const unsigned int p)
    {
        unsigned int i = 2;
        while( p * i <= n)
        {
            *( l + (i * p - 2) ) = 0;
            i++;
        }
    }
    
    void criba( unsigned int *l, const unsigned int n)
    {
        for( int i = 0;  i * i <= n ; i++ )
         if( IsPrime ( *( l + i) ) )
            setItem( l, n, *(l + i) );      
    }
    

提交回复
热议问题