返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.   cout<<"一到一萬的質數有"<<endl;
  7.   for(int i=2;i<=10000;i++)
  8.   {
  9.    int a=0;
  10.    for(int j=2;j<=i;j++)
  11.    {
  12.       if(i%j==0)
  13.       {
  14.          a++;
  15.       }
  16.    }
  17.    if(a==1)
  18.    {
  19.      cout<<i<<endl;
  20.    }     
  21.   }   
  22.   system("pause");  
  23.   return 0;
  24. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.   int b=0;
  7.   cout<<"一到一萬的質數有"<<endl;
  8.   for(int i=2;i<=10000;i++)
  9.   {
  10.    int a=0;
  11.    for(int j=2;j<=i;j++)
  12.    {
  13.       if(i%j==0)
  14.       {
  15.          a++;
  16.       }
  17.    }
  18.    if(a==1)
  19.    {
  20.      cout<<i<<endl;
  21.      b++;
  22.    }     
  23.   }   
  24.   cout<<"共有"<<b<<"個質數"<<endl;
  25.   system("pause");  
  26.   return 0;
  27. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int sum1=0;
  7.     for(int i=1;i<=10000;i++)
  8.     {
  9.       int sum=0;
  10.       for(int j=1;j<=i;j++)
  11.       {
  12.          if(i%j==0)
  13.          {
  14.              sum++;      
  15.          }
  16.      
  17.      }
  18.       if(sum==2)
  19.        {
  20.         sum1++;
  21.         cout<<i<<"\t";
  22.        }  
  23.     }
  24.     cout<<endl<<"質數共有"<<sum1<<"個"<<endl;
  25.     system("pause");
  26.     return 0;
  27. }
複製代碼

TOP

返回列表