PAT 07-3 evaluate prime number, pat07-3 evaluate Prime Number
Evaluate the prime number. This is an "old" problem. Everyone who has learned programming should have encountered it. Here is the classic problem of finding the prime numbers from M + 1 to N, of course, you have to write it down. The following are the question requirements and code implementation.
1/* 2 Name: 3 Copyright: 4 Author: 5 Date: 01/04/15 6 Description: 7 make Pi represent the I prime number. The current two positive integers M <= N <= 104. Please output all prime numbers from PM to PN. 8 9 input format: 10 11 Input M and N in a row, separated by spaces. 12 13 Output Format: 14 15 output all prime numbers from PM to PN. Each 10 digits occupies one row, which are separated by spaces, but no extra space is allowed at the end of the row. 16 17 input sample: 18 5 2719 output sample: 20 11 13 17 19 23 29 31 37 4321 47 53 59 61 67 71 73 79 83 8922 97 101 10323 */24 25 # include <stdio. h> 26 # include <math. h> 27 # include <stdbool. h> 28 29 void print (int M, int N); 30 bool isprime (int n); 31 32 int main () 33 {34 int M, N; 35 36 scanf ("% d", & M, & N); 37 print (M, N); 38 39 return 0; 40} 41 42 void print (int M, int N) 43 {44 int I, cnt; 45 46 for (I = 2, cnt = 0; cnt <N; I ++) 47 {48 if (isprime (I) 49 {50 cnt ++; 51 52 if (cnt> = M) 53 {54 printf ("% d", I); 55 if (cnt-M + 1) % 10! = 0 & cnt <N) 56 printf (""); 57 else58 printf ("\ n "); 59} 60} 61} 62} 63 64 bool isprime (int n) 65 {66 int I, tmp; 67 68 tmp = sqrt (n); 69 for (I = 2; I <= tmp; I ++) 70 {71 if (n % I = 0) 72 return false; 73} 74 75 return true; 76}