Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. /*
  2. * Prime finder, starts at 2 works up, was working initially
  3. * to 6658, now can run to 150000(highest tested yet) in ~17m25s.
  4. * May need optimization. Uses vector size to check for nth number.
  5. * Must have #include <vector>
  6. */
  7.  
  8. std::vector<int> prime; // required for program to work.
  9. int primes(unsigned y){
  10. using namespace std;
  11. unsigned b = 0;
  12. bool z = true; prime.push_back(2);
  13. for (b = 2; prime.size() <= y; b++){
  14. if (b % 2 == 0 && b < y){ b++; }//catches even numbers.
  15. //finds primes to the nth integer
  16. if (b >= 7){
  17. /*decrementing in this loop produced a strange
  18. *end count, used increments instead counts fixed
  19. *and program completes sooner.
  20. */
  21.  
  22. for (unsigned j = 2; j < prime.size(); j++){
  23. //if (j > y){ return 0; }//catches infinite loop
  24. if (prime.at(j) >= (b/2))
  25. //removes unneccessary checking
  26. {break;}
  27. if (b%prime.at(j) == 0)
  28. {z = false; j = prime.size();}
  29. }
  30. if (z == true){ prime.push_back(b); }
  31. z = true;
  32. }
  33. //inputs initial primes into array
  34. else if (b < 7){
  35. while (b <= 7){
  36. if (b == 2 || b == 3 || b == 5 || b == 7)
  37. {prime.push_back(b);} b++;
  38. }
  39. }
  40. }return 0;
  41. }
  42.  
  43. int prime[] = {0};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement