如何計算所有「素數」數字而不是顯示它們?在C++中計算素數
例子:
cout << "there are 125 prime numbers";
我使用的是1000號,因爲我想看看有多少素數有。
我不想顯示找到的素數,但我想知道有多少個被發現。
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <math.h>
#include <stdio.h>
using namespace std;
int main()
{
for (int a=2 ; a < 1000 ; a++)
{
bool prime = true;
for(int c=2 ; c*c <= a ; c++)
{
if(a % c == 0)
{
prime = false;
break;
}
}
if(prime) cout << a << " ";
}
return 0;
}
你格式化的人..它..漂亮壞。 – Rapptz
你知道這是所見即所得,當你寫下你的問題時,右邊有一盒格式化幫助,對吧? – chris
你爲什麼不真正以正確的方式提問這個問題。此外,您的問題可以用簡單的谷歌搜索來回答。是的,看看Eratosthenes的篩子。 – darxsys