Skip to content

Instantly share code, notes, and snippets.

Created December 26, 2012 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4378259 to your computer and use it in GitHub Desktop.
Save anonymous/4378259 to your computer and use it in GitHub Desktop.
n^2 - 2. Is it prime?
#include <iostream>
using namespace std;
#define MAX 100
bool isPrime(const int i)
{
if (i <= 1) return false;
int j;
for(j = 2; j*j <= i; j++)
if(0 == i%j) return false;
return true;
}
int main(void)
{
bool p;
char c;
int n2;
int odd;
for (odd = 3; odd < MAX; odd += 2)
{
n2 = (odd * odd) - 2;
p = isPrime(n2);
c = p ? ' ' : '*';
cout << odd << " : " << n2 << " " << c << " " << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment