统计信息

  • 访问数:8747
  • 博客数:142
  • 建立时间:2008-01-05
  • 更新时间:2008-05-21
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P5Q13

2008-04-09 09:36:44

天气: 晴朗 心情: 高兴

/*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;
}
 
}
加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd