check if it's prime or not(mersenne prime)

please, take a look at this
I wrote a code for printing Mersenne primes up to a given input by user.
a Mersenne number is a positive integer that is one less than a power of two (2^p-1)
#include<stdio.h>
main()
{
int a[9];
long n,b[20],p;
scanf("%ld",&n);
a[1]=2; a[2]=3;
a[3]=5; a[4]=7;
a[5]=11; a[6]=13;
a[7]=17; a[8]=19;
a[9]=23;
p=1;
for(int i=1; i<=19; i++)
{
p*=2;
b[i]=p-1;

}
i=1;
while((b[a[i]]<n)&&(i<9))
{
printf("%ld\n",b[a[i]]);
i++;
}
if(i==1) printf("baihgui\n");
}
input: 3000
output:
3
7
31
127
2047
but you know, 2047 isn't prime number(2047/23=89)
and i have to check b[a[i]] is prime or not. i tried, but it still prints 2047.

Just create a funtion to check if a number is prime:
1
2
3
4
bool IsPrime( long n )
{
     // For implementation see http://en.wikipedia.org/wiki/Primality_test
}

and print b[a[i]] only if ( IsPrime(b[a[i]]) )
Also you are indexing your "a" array [1] ... [9] but the valid indexes are [0] ... [8].

Topic archived. No new replies allowed.