The Mason number (Mersenne Prime) refers to a positive integer, in which exponent n is prime. If a mason is a prime number, it is called a mason prime. For example, all are Mason primes.
When n=2,3,5,7, all are primes, but when n=11, it is obviously not a mason prime.
In 1722, the Swiss master of Mathematics, Euler, proved to be a prime, with a total of 10 digits, the largest known prime number in the world at the time.
So far, only 47 primes have been found in humans. Mason primes has always been an important part in the study of number theory, and it is also a hot and difficult problem in scientific exploration nowadays.
Try to find all the prime primes of the exponential n<20. Problem Analysis
The problem to be solved programmatically is to find all the n<20 primes of the exponential. According to the definition of Mason prime number, we can first find out all the mason numbers of n<20, and then determine whether these numbers are prime. If it is a prime number, it means that it is a mason prime and can be printed out, otherwise it is not a mason prime. Algorithm Design
All the mason numbers of n<20 are required, so it is necessary to use the cyclic structure in the algorithm design of the subject.
Set the variable MP store Mason number, integer i expression index, and its value from 2〜19,i each change once, all corresponding to calculate a mason number, stored in the MP. The function prime () is invoked to determine the current MP value from each calculation.
When determining whether the MP is prime, you can define a function prime (), each time passing the current value of the MP as an argument to the function prime () and determining whether it is a prime number. If n is a prime number, the prime () function returns a value of 1, otherwise the prime () function returns a value of 0.
If the prime () function returns a value of 1, the current MP is a Mason prime and should be output, and if the prime () function returns a value of 0, the current MP is not a mason prime.
Program Flowchart:
Here's the complete code:
#include <math.h>
#include <stdio.h>
int prime (int n)
{
inti;
Longk;
K=SQRT (n) +1;
for (i=2;i<=k; i++)
if (n%i = 0) return
0;
return1;
}
int main ()
{
intmp, n=0, I;
printf ("mersenneprime:\n");
for (i=2;i<=20; i++)
{
Mp=pow (2,i)-1;
if (Prime (MP))
{
n++;
printf ("M (%d) =%d", I, MP);
printf ("\ n")
;
}
printf ("Thenumber of Mersenne Prime less than", N);
Return0;
}
Run Result:
Mersenne Prime:
M (2) =3
M (3) =7
M (5) =31
M (7) =127
M (13) =8191
M (17) =131071
M (19) =524287
The number of Mersenne Prime less than is:7
Hint: More excellent examples Please enter the C language Chinese network example, 100 C language examples free to share with you.