Advertisement
bhok

prime.cpp test

Oct 13th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #include "prime.h"
  2. #include "bitvect.h"
  3. #include <iostream>
  4.  
  5.  
  6. // constructor
  7. Prime::Prime(size_t ub) : bv_(ub + 1), bv_size_(ub)
  8. {
  9. Sieve();
  10. }
  11.  
  12. // destructor
  13. Prime::~Prime()
  14. {
  15. //delete &bv_;
  16. //delete &bv_size_;
  17. }
  18.  
  19. // copy constructor
  20. Prime::Prime(const Prime& cp) : bv_(cp.bv_size_ + 1), bv_size_(cp.bv_size_)
  21. {
  22. //bv_ = cp.bv_;
  23. }
  24.  
  25. // assignment operator
  26. Prime& Prime::operator= (const Prime& cp)
  27. {
  28. if (this != &cp)
  29. {
  30. delete &bv_;
  31. bv_ = cp.bv_;
  32. }
  33. return *this;
  34. }
  35.  
  36. void Prime::Sieve()
  37. {
  38. bv_.Unset(); // sets all numbers to 0
  39. size_t n = bv_.Size();
  40.  
  41. for (size_t p = 2; p*p <= n; p++)
  42. if (bv_.Test(p) == 0) // if numbers are backwards, needs to flip to 1
  43. for (size_t i = p * 2; i <= n; i += p)
  44. bv_.Set(i);
  45. }
  46.  
  47. size_t Prime::Largest(size_t ub) const
  48. {
  49. size_t inputNum = 0;
  50. size_t largest = 0;
  51.  
  52. // asks for input value
  53. std::cin >> inputNum;
  54.  
  55. // if input > n
  56. if (inputNum > ub)
  57. inputNum = ub;
  58. // loop until largest prime number is found
  59. for (size_t i = 0; i <= inputNum; i++)
  60. if (bv_.Test(i) == 1)
  61. largest = i;
  62.  
  63. return largest;
  64. }
  65.  
  66. void Prime::All(size_t ub, std::ostream& os) const
  67. {
  68. size_t inputNum = 0;
  69.  
  70. // asks for input value
  71. std::cin >> inputNum;
  72.  
  73. // if input > n
  74. if (inputNum >= ub)
  75. inputNum = ub;
  76.  
  77. // loop until largest prime number is found
  78. for (size_t i = 0; i <= inputNum; i++)
  79. if (bv_.Test(i) == 1)
  80. os << i << " ";
  81. }
  82. void Prime::All(std::ostream& os) const
  83. {
  84. for (size_t i = 0; i <= UpperBound(); i++)
  85. if (bv_.Test(i) == 1)
  86. os << i << " ";
  87. // same as previous but upperBound restricted
  88. }
  89. size_t Prime::UpperBound() const
  90. {
  91. // Is this all of it?
  92. size_t upperNum = 0;
  93. //bv_.Set();
  94.  
  95. // Do I need a loop to give me my prime number?
  96. for (size_t i = 0; i <= bv_.Size(); i++)
  97. upperNum = i;
  98.  
  99. return upperNum;
  100.  
  101. }
  102. void Prime::ResetUpperBound(size_t ub)
  103. {
  104. bv_.Expand(ub);
  105.  
  106. // A mini version of siev is required here
  107. }
  108.  
  109.  
  110. void Prime::Dump(std::ostream& os) const
  111. {
  112.  
  113. // DUMP NEEDS TO USE UPPERBOUND
  114.  
  115. os << '\t';
  116. for (size_t i = 0; i < bv_.Size(); ++i)
  117. os << bv_.Test(i);
  118. os << "\n\t";
  119. for (size_t i = 0; i < bv_.Size(); ++i)
  120. os << (i % 10);
  121. os << '\n';
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement