Java質數求解,java質數

來源:互聯網
上載者:User

Java質數求解,java質數

質數概念

  質數,又稱素數,指在一個大於1的自然數中,除了1和此整數自身外,無法被其他自然數整除的數(也可定義為只有1和本身兩個因數的數)。最小的素數是2,也是素數中唯一的偶數;其他素數都是奇數。質數有無限多個,所以不存在最大的質數。

目前總結大概有3中計算方式求解,具體如下

1. 粗魯暴力定義求解法

 

public class Prime {    // find the prime between 1 to 1000;    public static void main(String[] args) {        printPrime(1000);    }    public static void printPrime(int n) {        for (int i = 2; i < n; i++) {            int count = 0;            for (int j = 2; j <= i; j++) {                if (i % j == 0) {                    count++;                }                if (j == i && count == 1) {                    System.out.print(i + " ");                }                if (count > 1) {                    break;                }            }        }    }}

 

2. 平方根演算法(降低迴圈次數)

public class Prime {    // find the prime between 1 to 1000;    public static void main(String[] args) {        for (int i = 2; i < 1000; i++) {            if (isPrime(i)) {                System.out.print(i + " ");            }        }    }   /**     * 求指定數是否為質數     * @param num     * @return     */    public static boolean isPrime(int num) {        for (int j = 2; j <= Math.sqrt(num); j++) {            if (num % j == 0) {                return false;            }        }        return true;    }}

3. 規律法

  最小的素數是2,也是素數中唯一的偶數;其他素數都是奇數。質數有無限多個,所以不存在最大的質數。

 

public class Prime {    // find the prime between 1 to 1000;    public static void main(String[] args) {        List<Integer> primes = getPrimes(1000);        // 輸出結果        for (int i = 0; i < primes.size(); i++) {            Integer prime = primes.get(i);            System.out.printf("%8d", prime);            if (i % 10 == 9) {                System.out.println();            }        }    }   /**     * 求 n 以內的所有素數     * @param n  範圍     * @return n 以內的所有素數     */    private static List<Integer> getPrimes(int n) {        List<Integer> result = new ArrayList<Integer>();        result.add(2);        for (int i = 3; i <= n; i += 2) {            if (!divisible(i, result)) {                result.add(i);            }        }        return result;    }    /**     * 判斷 n 是否能被整除     * @param n       要判斷的數字     * @param primes  包含素數的列表     * @return        如果n能被 primes中任何一個整除,則返回 true。     */    private static boolean divisible(int n, List<Integer> primes) {        for (Integer prime : primes) {            if (n % prime == 0) {                return true;            }        }        return false;    }}

  第一種和第二種都是很簡單的方法:
  第三種方法說明了一個質數的特性:在所有質數中,只有2是偶數。如果一個數能夠被它之前的質數整除,那麼這個數不是質數。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.