[Functions]D. Liang 5.27 Mersenne prime.c

Description

A prime number is called a Mersenne prime if it can be written in the form of 2^p - 1 for some positive integer p.

Write a program that finds the number of Mersenne primes with p<=n, n is a specified integer.

Input

An interger n (1<=n<=15).

Output

The number of Mersenne primes with p <= n.

Sample Input

3

Sample Output

2
//   Date:2020/4/9
//   Author:xiezhg5
#include 
#include 
//判断是否素数 
int prime(int n)
{
    int i;
    long k;
    k = sqrt(n) + 1;
    for (i = 2; i <= k; i++)
    	if (n%i == 0)
    	    return 0;
        return 1;
}
int main(void)
{
    int mp, n, i,cnt=0;
    scanf("%d",&n);
    for (i = 2; i <= n; i++)
    {
    	//题目公式 
    	mp = pow(2, i) - 1;
	    if (prime(mp))  //判断是不是素数 
	    cnt++;  
    }
    printf("%d\n",cnt);  //输出p <= n的梅森素数的数量
    return 0;
}

你可能感兴趣的:(Matrix)