Skip to content

Instantly share code, notes, and snippets.

@mihids
Created September 3, 2014 06:40
Show Gist options
  • Save mihids/21927523d9bd85b9a30e to your computer and use it in GitHub Desktop.
Save mihids/21927523d9bd85b9a30e to your computer and use it in GitHub Desktop.
Prime Numbers
void printPrimeNumbers(unsigned int num) {
std::list<unsigned int> listPrimes;
listPrimes.push_back(2);
listPrimes.push_back(3);
listPrimes.push_back(5);
listPrimes.push_back(7);
std::string sNum;
unsigned int iNum;
unsigned int sqrt1;
for (unsigned int i = 10; i < num; i++) {
sNum = intToString(i);
iNum = charToInt(sNum[sNum.size() - 1]);
if ((iNum == 2) || (iNum == 4) || (iNum == 5) || (iNum == 6) || (iNum == 8) || (iNum == 0))
continue;
else if (((i % 3) == 0) || ((i % 7) == 0))
continue;
else {
sqrt1 = sqrt(i);
for (std::list<unsigned int>::iterator it = listPrimes.begin(); it != listPrimes.end(); it++) {
//std::cout << (*it) << ":" << sqrt1 << std::endl;
if ((*it) > sqrt1) {
listPrimes.push_back(i);
break;
}
if ((i % (*it)) == 0)
break;
}
}
}
std::cout << "Total: " << listPrimes.size() << std::endl;
unsigned int uiCount = 1;
std::string sSum = "0";
for (std::list<unsigned int>::iterator it = listPrimes.begin(); it != listPrimes.end(); it++) {
if ((*it) > 5000)
break;
std::cout << intToString(*it) << std::endl;
sSum = add(sSum, intToString(*it));
sSum = removePrecedingZeros(sSum);
//if (uiCount == 250)
// break;
uiCount++;
}
std::cout << "sum of numbers is: " << sSum << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment