Move Project Euler under puzzles.
This commit is contained in:
45
puzzles/project_euler/p007_10001st_prime.cpp
Normal file
45
puzzles/project_euler/p007_10001st_prime.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
/* Check cf5-opt.vim defs.
|
||||
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
|
||||
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
/*
|
||||
10001st prime
|
||||
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?
|
||||
|
||||
Solution:
|
||||
104743
|
||||
*/
|
||||
|
||||
typedef std::vector<long long> pt;
|
||||
pt p;
|
||||
|
||||
bool isPrime( long long n )
|
||||
{
|
||||
long h = (long long)sqrt(double(n));
|
||||
for ( pt::iterator i = p.begin(), ie = p.end();
|
||||
i!=ie && *i<=h;
|
||||
++i )
|
||||
if ( !(n%*i) )
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
int main ( void )
|
||||
{
|
||||
const long long n = 10001;
|
||||
|
||||
for ( long long i = 2; p.size() < n; ++ i )
|
||||
if ( isPrime( i ) )
|
||||
p.push_back( i );
|
||||
std::cout << p.back() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user