我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*fileName:P5Q13.java
*Name:Wang Chaoran
* Description:
13 (Prime number)
Write a program that meets the following requirements:
Declare a method to determine whether an integer is a prime number. Use the following method header:
public static boolean isPrime(int num)
An integer greater than 1 is a prime number if its only divisor is 1 or itself. For example, isPrime(11)
returns true, and isPrime(9) returns false.
Use the isPrime method to find the first thousand prime numbers and display every ten prime numbers in
a row, as follows:
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 ...
...
*/
public class P5Q13{
public static void main(String[] args){
int count = 0;
int num = 2;
while(count<1001){
if(isPrime(num)){
if(count%10==9)
System.out.println();
System.out.print(num+" ");
count++;
}
num++;
}
}
public static boolean isPrime(int num){
for(int i=2;i<=Math.sqrt(num);i++){
if(num%i==0)
return false;
}
return true;
}
}
导入论坛查看(32)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing