QUESTION: The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
I was trying to solve this problem. I was previously suggested to use the "Sieve of Eratosthenes" Method to find the prime nos.
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
I came up with the following code. The code compiled without errors and warnings. But it has generated a run time error.
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class factor
{
public:
int target;
float target1;
vector<int> buffer;
vector<int> prime;
vector<int>::iterator it;
void primeno();
void factors();
};
void factor::primeno()
{
target = 120;
target1 = 120.0;
int flagprime = 3, count = 1, flagtrip = 0;
for (int i=2; i<=target; i++)
{
buffer.push_back(i);
}
for (it = (buffer.begin()+count); it != buffer.end(); it++)
{
if ((*it)%2)
buffer.erase(it);
}
count+=1;
while (flagprime <= sqrt(target1))
{
for (it = (buffer.begin()+count); it != buffer.end(); it++)
{
if ((*it)%flagprime)
buffer.erase(it);
}
count+=1;
flagprime = *(buffer.begin()+count);
}
for (it = (buffer.begin()+count); it != buffer.end(); it++)
{
if ((*it)%flagprime)
buffer.erase(it);
}
cout << "Prime No" << endl << endl;
for (it = buffer.begin(); it != buffer.end(); it++)
{
cout << *it << "\t";
}
cout << endl << endl;
}
int main()
{
factor inst;
inst.primeno();
}
The error i obtained :
Debug Assertation failed!
Program : ......\project\Debug\Project.exe
File : C:\program files\microsoft visual studio 9.0\vc\include\vector
Line : 116
Expression : ("this->_Has_container()",0)
For information on how your program can cause an assertion failure, see the visual c++ documentations on asserts.
This is the part of the code that was present in the vector file at the line no mentioned during the error.
_Myt& operator++()
{ // preincrement
_SCL_SECURE_VALIDATE(this->_Has_container());
_SCL_SECURE_VALIDATE_RANGE(_Myptr < ((_Myvec *)(this->_Getmycont()))->_Mylast);
Plz point out where i went wrong and help me correct this code.