A snippet
if (a<=lim){
if(std::find(prims.begin(), prims.end(), a)==prims.end()){
prims.push_back(a);
count+=lim/a;
}
}
So basically I have this part in my code where I add the variable a into this vector if it isn’t already present, and then I update a counter on the fly.
But I am wondering if this is suboptimal in terms of runtime. Anything faster I can do?
std::setIs a container that is used commonly if you want to only keep unique values.Values are internally stored as red-black trees so most operations are of logarithmic complexity.
This would be faster than your implementation, and about as fast as the suggested binary search implementation.
You can improve it further by using the new (c++11)
std::unordered_set, which stores it as a hash table, which would make it even faster at average constant time operations.Use
setas followswhere TYPE is the type of the values stored in your vector