Home > Back-end >  This question is a bit difficult to ah, I'm fan! Save the children!
This question is a bit difficult to ah, I'm fan! Save the children!

Time:09-26

Subject for realization of a simple function of prime number and the use of the function to determine whether a given interval integer prime Numbers, and output function, according to a row of five

Prime: only positive integer divisible by 1 and itself, is not a prime number 1, 2 is a prime number

Function interface definition:

Int IsPrime (int x);

M void PrintPrime (int, int n);

IsPrime () function, when x is a prime number return 1, otherwise it returns 0. Function PrintPrime output range within [m, n] all prime Numbers, according to a row of five output, if not a prime number in this interval, then output "does not exist within the range of the prime",

Requirement: the main function of the input of two positive integers m and n, the calling function PrintPrime output, IsPrime function calls a function PrintPrime need to decide in the interval [m, n] whether an integer is prime,

CodePudding user response:

Fans have to write one line of code you are not to come out...

CodePudding user response:

This kind of problem baidu a lot of

CodePudding user response:

Please click my blog

CodePudding user response:

For (j=n; j<=m; j++);
For (I=2, isprime=true; i<=j/2; I++)
{
If (j % I==0)
Isprime=false;
}
Return isprime;

Judging prime code so give it a try I don't guarantee for other plus the baidu, a lot of this subject

CodePudding user response:

reference 4 floor newshou1 response:
for (j=n; j<=m; j++);
For (I=2, isprime=true; i<=j/2; I++)
{
If (j % I==0)
Isprime=false;
}
Return isprime;

Judge prime code that is try I don't promise to other plus for the title baidu many

This code, when n is 1, 1 is also a prime number?

CodePudding user response:

1 is not a prime number nor a composite
Can add a if after the first for loop (n==1)
Isprime=true;
The else
{
The second for loop
}

CodePudding user response:

Write the wrong n==1 isprime=false;

CodePudding user response:

. Isprime=false behind add a break; Jump out of the first cycle

CodePudding user response:

# include & lt; Stdio. H>

Int IsPrime (int x)
{
If (x & lt; 2) return 0;
For (int I=2; i {
If (x % I==0)
return 0;
}
return 1;
}

Void PrintPrime (int m, int n)
{
int count=0;
Int k=0;
For (int I=m; i <=n; I++)
{
If (IsPrime (I))
{
Printf (" % d ", I);
K++;
count++;
}
If (k==5)
{
printf("\n");
K=0;
}
}
if(! The count) printf (" no prime in the area ");
}

Int main ()
{
Int m, n;
Printf (" input two number: ");
The scanf (" % d % d ", & amp; M, & amp; N);
If (m & lt; N)
{
PrintPrime (m, n);
}
The else
{
PrintPrime (n, m);
}
return 0;
}
  • Related