Created
April 25, 2013 11:42
-
-
Save hhc0null/5459143 to your computer and use it in GitHub Desktop.
i want to take a bath immediately.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <iostream> | |
#include <iomanip> | |
#include <stack> | |
#include <list> | |
#include <vector> | |
int main(void) { | |
std::list<int> input; | |
std::vector<int> pnums; | |
int buf; | |
// generate prime numbers, using sqrt. | |
pnums.push_back(2); | |
pnums.push_back(3); | |
for(int i = 5; i < 1000000; i+=2) { | |
buf = std::sqrt(i); | |
bool flag = true; | |
std::vector<int>::iterator vitr = pnums.begin(); | |
for(;*vitr <= buf && vitr != pnums.end(); ++vitr) { | |
if(i % *vitr == 0) { | |
flag = false; | |
break; | |
} | |
} | |
if(flag) { | |
pnums.push_back(i); | |
} | |
} | |
// test code. | |
/* | |
std::vector<int>::iterator vitr = pnums.begin(); | |
for(;vitr != pnums.end(); vitr++) { | |
std::cout << *vitr << std::endl; | |
} | |
*/ | |
// input | |
for(;std::cin >> buf;) { | |
input.push_back(buf); | |
} | |
// search. | |
std::list<int>::iterator litr = input.begin(); | |
for(;litr != input.end(); ++litr) { | |
int i; | |
bool flag; | |
std::vector<int>::iterator vitr = pnums.begin(); | |
for(i = 0, flag = true;vitr != pnums.end(); ++vitr, ++i) { | |
if(*vitr > *litr) break; | |
} | |
// output | |
std::cout << i << std::endl; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment