Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "prime.h"
- #include "bitvect.h"
- #include <iostream>
- // constructor
- Prime::Prime(size_t ub) : bv_(ub + 1), bv_size_(ub)
- {
- Sieve();
- }
- // destructor
- Prime::~Prime()
- {
- //delete &bv_;
- //delete &bv_size_;
- }
- // copy constructor
- Prime::Prime(const Prime& cp) : bv_(cp.bv_size_ + 1), bv_size_(cp.bv_size_)
- {
- //bv_ = cp.bv_;
- }
- // assignment operator
- Prime& Prime::operator= (const Prime& cp)
- {
- if (this != &cp)
- {
- delete &bv_;
- bv_ = cp.bv_;
- }
- return *this;
- }
- void Prime::Sieve()
- {
- bv_.Unset(); // sets all numbers to 0
- size_t n = bv_.Size();
- for (size_t p = 2; p*p <= n; p++)
- if (bv_.Test(p) == 0) // if numbers are backwards, needs to flip to 1
- for (size_t i = p * 2; i <= n; i += p)
- bv_.Set(i);
- }
- size_t Prime::Largest(size_t ub) const
- {
- size_t inputNum = 0;
- size_t largest = 0;
- // asks for input value
- std::cin >> inputNum;
- // if input > n
- if (inputNum > ub)
- inputNum = ub;
- // loop until largest prime number is found
- for (size_t i = 0; i <= inputNum; i++)
- if (bv_.Test(i) == 1)
- largest = i;
- return largest;
- }
- void Prime::All(size_t ub, std::ostream& os) const
- {
- size_t inputNum = 0;
- // asks for input value
- std::cin >> inputNum;
- // if input > n
- if (inputNum >= ub)
- inputNum = ub;
- // loop until largest prime number is found
- for (size_t i = 0; i <= inputNum; i++)
- if (bv_.Test(i) == 1)
- os << i << " ";
- }
- void Prime::All(std::ostream& os) const
- {
- for (size_t i = 0; i <= UpperBound(); i++)
- if (bv_.Test(i) == 1)
- os << i << " ";
- // same as previous but upperBound restricted
- }
- size_t Prime::UpperBound() const
- {
- // Is this all of it?
- size_t upperNum = 0;
- //bv_.Set();
- // Do I need a loop to give me my prime number?
- for (size_t i = 0; i <= bv_.Size(); i++)
- upperNum = i;
- return upperNum;
- }
- void Prime::ResetUpperBound(size_t ub)
- {
- bv_.Expand(ub);
- // A mini version of siev is required here
- }
- void Prime::Dump(std::ostream& os) const
- {
- // DUMP NEEDS TO USE UPPERBOUND
- os << '\t';
- for (size_t i = 0; i < bv_.Size(); ++i)
- os << bv_.Test(i);
- os << "\n\t";
- for (size_t i = 0; i < bv_.Size(); ++i)
- os << (i % 10);
- os << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement