Printing prime numbers from 1 through 100

前端 未结 22 2337
无人共我
无人共我 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 05:43
    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    void main()
    { int f =0;
     for(int i=2;i<=100;i++)
      {
       f=0;
       for(int j=2;j<=i/2;j++)
       { 
         if(i%j==0)
         { f=1;
           break;
         }
       }
     if (f==0)
      cout<<i<<" ";
    }
     system("pause");
    }
    
    0 讨论(0)
  • 2020-11-28 05:46

    Using the rules of divisibility prime numbers can be found out in O(n) and it`s really effecient Rules of Divisibility

    The solution would be based on the individual digits of the number...

    0 讨论(0)
  • 2020-11-28 05:47

    I always use this one (it's easy and fast) :

    #include <iostream>
    using namespace std;
    
    int i,j;
    bool b[101];
    
    int main( )
    {
        for(i=2;i<101;i++){
            b[i]=true;
        }
        for(i=1;i<101;i++){
            if(b[i]){
                cout<<i<<" ";
                for(j=i*2;j<101;j+=i) b[j]=false;
            }
        }
    }
    

    Here is output of this code: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

    0 讨论(0)
  • 2020-11-28 05:47

    While this is relatively more production grade prime number generator, it is still valid to use for finding prime numbers from 1 through 100. The code uses Miller-Rabin Primality Test to achieve calculate prime numbers. Since it is probabilistic method, the accuracy increases with value of k. While the focus is on readability of code rather than speed, on AWS r5.2xlarge instance it took 3.791s for prime number until 1,000,000.

    // C++ program to print all primes smaller than or equal to 
    // n using Miller-Rabin Primality Test
    // Reference: https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
    // It is not particularly to optimized 
    // since focus is readability
    // Compile: g++  -std=c++17 -o prime prime.c++ -ltbb 
    
    #include <execution>
    #include <iostream>
    #include <math.h>
    using namespace std; 
    
    int power(unsigned long int x, unsigned long y, unsigned long p){
        int res = 1;
        x = x % p;
        while (y > 0) {
            if (y & 1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }
    
    bool millerTest(unsigned long d, unsigned long n) {
        unsigned long a = 2 + rand () % (n - 4);
    
        unsigned long x = power(a, d, n);
    
        if (x == 1  || x == n - 1)
            return true;
    
        while (d != n - 1){
            x = (x * x) % n;
            d *= 2;
            if (x == 1) return false;
            if (x == n - 1) return true;
    
        }
        return false;
    }
    
    bool isPrime(unsigned long n, int k) {
        if (n <= 1 || n == 4) return false;
        if (n <= 3) return true;
    
        unsigned long int d = n - 1;
        while (d % 2 == 0)
            d /= 2;
        for(int i = 0; i < k; i++){
            if (!millerTest(d, n))
                return false;
        }
        return true;
    }
    
    
    
    
    int main() 
    { 
        int n = 1000000;
        int k = 200; 
        vector<unsigned long> primeN(n);
        iota(primeN.begin(), primeN.end(), 1);
    
        vector<bool> isPrimeV(n);
    
    
    
        transform(execution::par,
            primeN.begin(), primeN.end(), 
            isPrimeV.begin(), 
            [k](unsigned long x) -> bool {
                return isPrime(x, k);
            });
    
        int count = accumulate(isPrimeV.begin(), isPrimeV.end(), 0, [](int d, bool v){
            if (v == true) return d += 1; else return d;
        });
        cout << count << endl;
    
        return 0; 
    } 
    
    
    0 讨论(0)
  • 2020-11-28 05:49

    here is a simple code for printing all the prime numbers until given number n,

    #include<iostream.h>
    #include<conio.h>
    
    void main()
    {
    clrscr();
    int n,i,j,k;
    cout<<"Enter n\n";
    cin>>n;
    
    for(i=1;i<=n;i++)
    {   k=0;
      for(j=1;j<=i;j++)
      {
        if((i%j)==0)
        k++;
       }
      if(k==2)
      cout<<i<<endl;
    }
    getch();
    }
    
    0 讨论(0)
  • 2020-11-28 05:51

    This is my very simple c++ program to list down the prime numbers in between 2 and 100.

    for(int j=2;j<=100;++j)
    {
        int i=2;
        for(;i<=j-1;i++)
        {
            if(j%i == 0)
                break;
        }
    
        if(i==j && i != 2)
            cout<<j<<endl;
    }
    
    0 讨论(0)
提交回复
热议问题