Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Prime finder, starts at 2 works up, was working initially
- * to 6658, now can run to 150000(highest tested yet) in ~17m25s.
- * May need optimization. Uses vector size to check for nth number.
- * Must have #include <vector>
- */
- std::vector<int> prime; // required for program to work.
- int primes(unsigned y){
- using namespace std;
- unsigned b = 0;
- bool z = true; prime.push_back(2);
- for (b = 2; prime.size() <= y; b++){
- if (b % 2 == 0 && b < y){ b++; }//catches even numbers.
- //finds primes to the nth integer
- if (b >= 7){
- /*decrementing in this loop produced a strange
- *end count, used increments instead counts fixed
- *and program completes sooner.
- */
- for (unsigned j = 2; j < prime.size(); j++){
- //if (j > y){ return 0; }//catches infinite loop
- if (prime.at(j) >= (b/2))
- //removes unneccessary checking
- {break;}
- if (b%prime.at(j) == 0)
- {z = false; j = prime.size();}
- }
- if (z == true){ prime.push_back(b); }
- z = true;
- }
- //inputs initial primes into array
- else if (b < 7){
- while (b <= 7){
- if (b == 2 || b == 3 || b == 5 || b == 7)
- {prime.push_back(b);} b++;
- }
- }
- }return 0;
- }
- int prime[] = {0};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement