Skip to content

Instantly share code, notes, and snippets.

@qpfiffer
Created February 16, 2012 06:08
Show Gist options
  • Select an option

  • Save qpfiffer/1842563 to your computer and use it in GitHub Desktop.

Select an option

Save qpfiffer/1842563 to your computer and use it in GitHub Desktop.
Prime number thing
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
bool isPrime (long long n)
{
bool tempbool = true;
if (n <= 3) {
return true;
}
else if (n % 2 == 0) {
return false;
}
int sqrtN = (int)sqrt((long double)n);
for (int i = 3; i <= sqrtN; i++) {
if (n % i == 0)
return false;
}
// End of the world condition.
return true;
}
int main()
{
long i = 2;
long double j = 0;
long double templogs = 0;
long double logs1 = 0;
long double logs2 = 0;
long double logs3 = 0;
long double logs4 = 0;
long double logs5 = 0;
long double logs6 = 0;
int n = 100;
bool shouldExit = false;
while (shouldExit == false)
{
// Start at two because less is always prime. Or negative.
for (; i <= n; i++)
{
if (isPrime(i))
{
j = i;
if ( n == 100 )
cout << i << " ";
templogs += log(j);
}
}
switch (n)
{
case 100: //one hundred
n = 1000; //one thousand
logs1 = templogs;
break;
case 1000: //one thousand
n = 10000; //ten thousand
logs2 = templogs;
break;
case 10000: //ten thousand
n = 100000; //one hundred thousand
logs3 = templogs;
break;
case 100000: //one hundred thousand
n= 1000000; //one million
logs4 = templogs;
break;
case 1000000: //one million
n = 10000000; //ten million
logs5 = templogs;
break;
case 10000000: //ten million
logs6 = templogs;
shouldExit = true;
break;
}
}
cout << fixed << "\n\n" << setw(15) << "Sum of logs" << setw(9) << "n" << setw(9) << "Ratio" << endl;
cout << fixed << setw(15) << logs1 << setw(9) << "100" << setw(9) << "Ratio" << endl;
cout << fixed << setw(15) << logs2 << setw(9) << "1000" << setw(9) << "Ratio" << endl;
cout << fixed << setw(15) << logs3 << setw(9) << "10000" << setw(9) << "Ratio" << endl;
cout << fixed << setw(15) << logs4 << setw(9) << "100000" << setw(9) << "Ratio" << endl;
cout << fixed << setw(15) << logs5 << setw(9) << "1000000" << setw(9) << "Ratio" << endl;
cout << fixed << setw(15) << logs6 << setw(9) << "10000000" << setw(9) << "Ratio" << endl;
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment