PAT primes B -1013 (20 minutes)

Title:
Order P i denotes the i-th prime number. The current to the power of two positive integers M≤N≤10 4, output a P (M) to all prime numbers P (N) is.

Input format:
input given M and N in a row, separated by a space therebetween.

Output format:
output from the P (M) to all prime numbers P (N), each representing a digit line 10, separated by spaces therebetween, the end of the line may not have extra space.

Sample input:

5 27

Sample output:

11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103

My code:

#include<cstdio>
#include<math.h>
typedef long long LL;
bool prime(LL n){
    if(n<=1)
        return false;
    LL sqr=(int) sqrt(n);
    for(LL i=2;i<sqr+1;i++)
        if(n%i==0)
            return false;
    return true;
}
int main(){
    int k=0,j=0;
    LL m,n;
    scanf("%d %d",&m,&n);
    for(LL i=2;i<10000000;i++){
        if(prime(i)){
            k++;
            if(k>=m&&k<=n){
                printf("%d",i);
                j++;
                if(k>=n)
                    break;
                if(j%10!=0)
                    printf(" ");
                else
                    printf("\n");
            }
        }
    }
    return 0;
}

Published 13 original articles · won praise 0 · Views 146

Guess you like

Origin blog.csdn.net/qq_34451909/article/details/104689228