Home > Back-end >  O n primes between m and the output
O n primes between m and the output

Time:09-17

Title description

O n to m within the prime (containing n, m, and the m>=n>=1, n<=m<(n=100) and m by keyboard input behind each prime number with a space)

Requirements: (1) write the following interface functions: int fun (int n), the function of the function is to determine n is a prime number, if yes, return 1, if it is not return 0,

(2) the main function to call the function function,
Enter

7, 15 (note with number)
O

7, 11, 13

CodePudding user response:

This is very basic.

CodePudding user response:

# include
# include

Int main ()
{
Int N, M, x=2, sum=0;
Int I, isPrime, CNT=0;
The scanf (" % d % d ", & amp; N, & amp; M);
If (N> 0 & amp; & N<=M & amp; & M<{
=200)While (cntIsPrime=1;
For (I=2; i<=SQRT (x); I++) {
If I==0) (x % {
IsPrime=0;
break;
}
}
If (isPrime==1) {
Cnt++;
If ((cnt>=N) & amp; & (isPrime==1))
Sum=sum + x;
}
X++;
}
}
Printf (" % d ", sum);

return 0;
}

CodePudding user response:

Only can optimize the prime number:
 
#include
#include

The inline int isprime (int n)
{
If (n==2 | | n==3) return 1;
If (n & lt; 5) return 0;
If (n % 6!=1 & amp; & N % 6!=5) return 0;

For (int I=3; I & lt;=SQRT (n) (float); I +=2)
if (! (I) n %) return 0;

return 1;
}

Int main ()
{
Int n, m;

The scanf (" % d, % d ", & amp; N, & amp; M);
If (n & gt; 0 & amp; & M & gt;=n)
{
for (; N & lt;=m; N++)
If (isprime (n)) printf (" % d ", n);
Puts (" ");
}

return 0;
}

  • Related