http://projecteuler.net/problem=7 【求第10001个素数】

来源:互联网 发布:网络推广的作用 编辑:程序博客网 时间:2024/05/30 05:09
#include <iostream>#include <vector>using std::cout;using std::endl;using std::vector;vector<uint32_t> ivec;bool prime( uint32_t n ){    vector<uint32_t>::iterator it = ivec.begin();    for ( it; it != ivec.end() && *it * *it <= n ; ++it )    {        if ( !(n % *it) )        {            return false;        }    }    return true;}//http://projecteuler.net/problem=7//By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.//What is the 10 001st prime number?uint32_t resolve_P7( uint32_t m ){    for ( uint32_t i = 2; ivec.size() < m; ++i )    {        if ( prime ( i ) )        {            //cout << i << endl;            ivec.push_back(i);        }    }    if ( !ivec.empty() )    {        return *(ivec.end() - 1);    }    return 0;}int main(){    uint32_t result = 10001;    result = resolve_P7(result);    cout << "result:\t" << result << endl; //104743    return 0;}

原创粉丝点击