Printing prime numbers from 1 through 100

前端 未结 22 2340
无人共我
无人共我 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<int>(
          std::sqrt( static_cast<double>( 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 <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include <cmath>
    
    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<int>(
          std::sqrt( static_cast<double>( 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) );      
    }
    
    0 讨论(0)
  • 2020-11-28 06:06

    this is my approach from a simple blog:

    //Prime Numbers generation in C++
    //Using for loops and conditional structures
    #include <iostream>
    using namespace std;
    
    int main()
    {
    int a = 2;       //start from 2
    long long int b = 1000;     //ends at 1000
    
    for (int i = a; i <= b; i++)
    {
    
     for (int j = 2; j <= i; j++)
     {
        if (!(i%j)&&(i!=j))    //Condition for not prime
            {
                break;
            }
    
        if (j==i)             //condition for Prime Numbers
            {
                  cout << i << endl;
    
            }
     }
    }
    }
    

    - See more at: http://www.programmingtunes.com/generation-of-prime-numbers-c/#sthash.YoWHqYcm.dpuf

    0 讨论(0)
  • 2020-11-28 06:07

    A simple program to print "N" prime numbers. You can use N value as 100.

        #include  <iostream >
        using  namespace  std;
    
        int  main()
        {
            int  N;
            cin  >>  N;
            for (int  i =  2;  N > 0;  ++i)
            {
                bool  isPrime  =  true ;
                for (int  j =  2;  j < i;  ++j)
                {
                    if (i  % j ==  0)
                    {
                        isPrime  =  false ;
                        break ;
                    }
                }
                if (isPrime)
                {
                    --N;
                    cout  <<  i  <<  "\n";
                }
            }
            return  0;
        }
    
    0 讨论(0)
  • 2020-11-28 06:08

    actually the better solution is to use "A prime sieve or prime number sieve" which "is a fast type of algorithm for finding primes" .. wikipedia

    The simple (but not faster) algorithm is called "sieve of eratosthenes" and can be done in the following steps(from wikipedia again):

    1. Create a list of consecutive integers from 2 to n: (2, 3, 4, ..., n).
    2. Initially, let p equal 2, the first prime number.
    3. Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked.
    4. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.
    0 讨论(0)
提交回复
热议问题