Pat 数素数(20)

来源:互联网 发布:pci串行端口驱动下载 编辑:程序博客网 时间:2024/06/05 16:19

1013. 数素数 (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

令Pi表示第i个素数。现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数。

输入格式:

输入在一行中给出M和N,其间以空格分隔。

输出格式:

输出从PM到PN的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。

输入样例:
5 27
输出样例:
11 13 17 19 23 29 31 37 41 4347 53 59 61 67 71 73 79 83 89

97 101 103

注意:还是会超时。

import java.util.Scanner;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in);int a = sc.nextInt();int b = sc.nextInt();int w = 0;for (int i = 2;; i++) {if (isprime(i) == true) {w++;if (w >= a) {if ((w - a + 1) % 10 == 0 && w != a) {System.out.println(i);} else {System.out.print(i + " ");}}if (w == b) {System.out.print(i);break;}}}}public static boolean isprime(int q) {if (q % 2 == 0 && q != 2) {return false;}if (q % 3 == 0 && q != 3) {return false;}if (q % 5 == 0 && q != 5) {return false;}for (int i = 2; i <= Math.sqrt(q); i++) {if (q % i == 0) {return false;}}return true;}}


原创粉丝点击