2012年9月15日 星期六

Prime(Basic Method)

C++ code colored by C++2HTML
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    bool b;
    int i,j,end, ans[100] = {2};
    end = 1;
    for(i=3;;i+=2)
    {
        b = false;
        for(j=0;j<end;j++)
        {
            if(0 == i%ans[j])
            {
                b = true;
                break;
            }
        }
        if(false == b)
        {
            ans[end] = i;
            ++end;
        }
        if(100 == end)break;
    }
    for(j=0;j<end;j++)cout<<ans[j]<<endl;//print 100 prime numbers
    system("PAUSE");
    return 0;
}

個人合成作品