//this is a mine...sieve's algorithm implementation please tell me where i am going wrong...//
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int a,b,p;
    vector<int> v;
    cin>>a>>b;
    for(int i=a;i<=b;i++)
    v.push_back(i);//inserting all values upto given limit in a vector..
    for(p=2;(p*p)<=b;p++)
    {
                     for(int n=2;(n*p)<=b;n++)
                     {
                             int s=(n*p);
                             v.erase(v.begin()+s);//erase all multiples of 2,3....from vector
                     }
                     }
                     for(int i=0;i<v.size();i++)
                     cout<<v[i]<<endl;
                     }

You're erasing elements by position rather than by value. Once you start erasing values like that, you can't expect v[number] to be equal to number anymore. A better solution to this problem would be to have an array of bool keep track of which numbers can be prime, and which numbers can't. Instead of removing values from an array, you would just set them to false.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.