Prime number generation algorithm

前端 未结 3 610
渐次进展
渐次进展 2020-12-21 13:02

Please look at the following and see if you could advise.

cout << \"2\" << endl;
cout << \"3\" << endl;

ofstream of(\"Primes.txt\");         


        
3条回答
  •  一个人的身影
    2020-12-21 13:47

    here is the most simplest logic:

    //Prime Numbers generation in C++
    //Using for loops and conditional structures
    #include 
    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;
    
            }
     }
    }
    }
    

提交回复
热议问题