Topic content:
We think 2 is the first prime number, 3 is the second Prime, 5 is the third prime, and so on.
Now, given the two integers n and m,0<n<=m<=200, your program calculates the number of primes between the nth prime number and the number of M primes, including the nth Prime and the first M prime.
Input format:
Two integers, the first representing N, and the second represents M.
Output format:
An integer representing the nth prime number and the number of primes between the first m primes, including the nth Prime and the first M prime.
Input Sample:
2 4
Sample output:
15
Time limit: 500ms memory limit: 32000kb
int n,m; The first few prime scanf ("%d", &n); scanf ("%d", &m); int sum=0; The nth prime number to the and int a=0 of the first M primes ;//Record the first few primes of int j=2;//Record the value of the number a primes do { int b=1; for (int i=2;i<= (int) sqrt (j); i++) { if ((j%i==0) &&j>2) { b=0;//execute then current J is not a prime number Break Jump out of this loop } } if (b)//Is True, the current J is the prime number, then a count { a++; if (a>=n && a<=m)//pairs of primes between N and M are counted and sum { sum=sum+j; System.out.println (j+ "," +a); } } j + +; } while (a<m); printf ("%d\n", sum);
Use case test Results |
Run time |
Memory consumption |
Tips |
Score |
Use Case 1 by |
1ms |
256kb |
|
1 |
Use Case 2 by |
13ms |
256kb |
|
1 |
Use Case 3 by |
1ms |
256kb |
|
1 |
Use Case 4 by |
1ms |
256kb |
|
1 |
Use Case 5 by |
1ms |
144kb |
|
1 |
Submit an Answer
Score/Score:5.00/5.00 min
Introduction to Programming--c Language 4th week programming Exercise 1 primes and (5 points)