Archive for the ‘Data Structure’ Category

求自然数n以内的质数

今天遇到这样一道题目:求自然数n以内的质数
经过观察以后,我觉得可以以下方式来求解:
将已求解的质数放入结果数组,判断下一个数是否是质数,只要将它除以质数结果数组,如果都不能被整除,那么这个数也是一个质数,放入结果数组中。
/*
* PrimeNumber.java
*
* Created on 2006年8月12日, 下午6:05
*
* This class is under Gnu GPL.
*/

package primenumber;

import java.io.IOException;

/**
* Get prime numbers.
*
* @author <a href=”guangquanzhang@gmail.com“>guangquanzhang</a>
*/
public class PrimeNumber {

/** Creates a new instance of PrimeNumber */
public PrimeNumber() {
}

/**
* @param n natural number, 1-n
* @return all prime numbers [...]

By javafuns on April 24, 2007 at 00:15 · Views: 1,429 · Permalink · 3 Comments
Categorized in: Data Structure, Java · Tagged with: ,
  • Highest Rated

  • My PicasaPhotos

    IMG_0594.JPG

    IMG_0627.JPG

    IMG_0567.JPG

  • RSS My del.icio.us

  • My RSS