public
anonymous / n2 primer.cpp
Created

n^2 - 2. Is it prime?

  • Download Gist
n2 primer.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.