{"id":492,"date":"2019-08-15T23:51:58","date_gmt":"2019-08-15T15:51:58","guid":{"rendered":"http:\/\/aisaka.xyz\/?p=492"},"modified":"2019-08-15T23:51:58","modified_gmt":"2019-08-15T15:51:58","slug":"%e8%8e%b7%e5%8f%96n%e4%bb%a5%e5%86%85%e7%9a%84%e8%b4%a8%e6%95%b0prime","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=492","title":{"rendered":"\u83b7\u53d6N\u4ee5\u5185\u7684\u8d28\u6570(Prime)"},"content":{"rendered":"\n<p>\u53ea\u8981\u4ed4\u7ec6\u60f3\u4e00\u60f3\u5c31\u80fd\u5199\u51fa\u6765\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u5f97\u51fa\u7ed3\u679c\u5bb9\u6613\uff0c\u5f97\u51fa\u7ed3\u679c\u82b1\u8d39\u7684\u65f6\u95f4\u5c31\u4e0d\u4e00\u6837\u4e86\u3002\u4e3a\u4e86\u5bf9\u6bd4\u51fa\u6548\u679c\uff0cN\u53d6100000\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u65b9\u6cd5\u4e00\uff1a<\/p><\/blockquote>\n\n\n\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/**\n * @Author Diuut\n * @Date 2019\/8\/15  20:54\n *\/\npublic class Test0814_1_Prime {\n    public static void main(String[] args) {\n        long start = System.currentTimeMillis();\n        int j;\n        for(int i = 2;i&lt;=100000;i++) \/\/ 1\u4e0d\u662f\u7d20\u6570\uff0c\u6240\u4ee5\u76f4\u63a5\u4ece2\u5f00\u59cb\u5faa\u73af\n        {\n            j = 2;\n            while (i % j != 0)\n                j++; \/\/ \u6d4b\u8bd52\u81f3i\u7684\u6570\u5b57\u662f\u5426\u80fd\u88abi\u6574\u9664\uff0c\u5982\u4e0d\u80fd\u5c31\u81ea\u52a0\n            if (j == i) \/\/ \u5f53\u6709\u88ab\u6574\u9664\u7684\u6570\u5b57\u65f6\uff0c\u5224\u65ad\u5b83\u662f\u4e0d\u662f\u81ea\u8eab\n                System.out.println(i); \/\/ \u5982\u679c\u662f\u5c31\u6253\u5370\u51fa\u6570\u5b57\n        }\n        long end = System.currentTimeMillis();\n        System.out.println(&quot;\u672c\u6b21\u8fd0\u884c\u8017\u65f6:&quot;+(end-start));\n    }\n}\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\u672c\u6b21\u8fd0\u884c\u8017\u65f6:3736<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u65b9\u6cd5\u4e8c<\/p><\/blockquote>\n\n\n\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport java.util.ArrayList;\nimport java.util.List;\n\/\/\u68c0\u9a8c\u4e00\u4e2a\u6570\u662f\u4e0d\u662f\u7d20\u6570\uff0c\u5982\u679c\u6240\u6709\u5c0f\u4e8e\u5b83\u7684\u7d20\u6570\u90fd\u4e0d\u80fd\u5c06\u5b83\u6574\u9664\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u7d20\u6570\npublic class Test0814_2_Prime {\n    public static void main(String[] args) {\n        long start = System.currentTimeMillis();\n        List&lt;Integer&gt; primes=getPrimes(100000);\n\n\n        for(int i=0;i&lt;primes.size();i++) {\n            Integer prime=primes.get(i);\n            System.out.println(prime+&quot; &quot;);\n\n        }\n        long end = System.currentTimeMillis();\n        System.out.println(&quot;\u672c\u6b21\u8fd0\u884c\u8017\u65f6\uff1a&quot;+(end-start));\n    }\n\n    \/*\n     * \u6c42n\u4ee5\u5185\u7684\u6240\u6709\u7d20\u6570\n     *\n     *\/\n    private static List&lt;Integer&gt; getPrimes(int n){\n        List&lt;Integer&gt; result =new ArrayList&lt;Integer&gt;();\n        result.add(2);\/\/\u7b2c\u4e00\u4e2a\u7d20\u6570\u5148\u653e\u5165\n        for(int i=3;i&lt;=n;i+=2) {  \/\/\u904d\u5386\uff0c\u51cf\u5c11\u5faa\u73af\u6b21\u6570\uff0c\u6b65\u957f\u4e3a2\n            if(!divisble(i,result)) {  \/\/\u5224\u65ad\u662f\u5426\u6709\u7d20\u6570\u80fd\u88ab\u6574\u9664\n                result.add(i);     \/\/\u5982\u679c\u4e0d\u80fd\u6574\u9664\uff0c\u52a0\u5165\u5217\u8868List\n            }\n        }\n        return result;  \/\/\u8fd4\u56de\u5217\u8868list\n    }\n\n\n    \/*\n     * \u5224\u65adn\u80fd\u5426\u80fd\u88ab\u6574\u9664\n     *\n     *\/\n    private static boolean divisble(int n,List&lt;Integer&gt; primes) {\n        for(Integer prime:primes) {  \/\/\u904d\u5386\u5217\u8868List\n            if(n % prime == 0) {\n                return true;\n            }\n            if(prime&gt;=Math.sqrt(n))   \/\/\u5982\u679c\u8d85\u8fc7\u5e73\u65b9\u6839\uff0c\u8fd8\u6ca1\u627e\u5230\u6574\u9664\uff0c\u9000\u51fa\u5faa\u73af\n                break;\n\n        }\n        return false;\n    }\n}\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\u672c\u6b21\u8fd0\u884c\u8017\u65f6\uff1a375<\/pre>\n\n\n\n<p>\u7ed3\u679c\u975e\u5e38\u660e\u663e\uff0c\u4e0d\u540c\u7684\u5199\u6cd5\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u90fd\u662f\u4e00\u6837\u7684\uff0c\u4f46\u662f\u82b1\u8d39\u7684\u65f6\u95f4\u5374\u662f\u5929\u58e4\u4e4b\u522b\u3002\u4e00\u7ec4\u4ee3\u7801\u5dee1\u79d2\uff0c\u4e00\u6574\u4e2a\u9879\u76ee\u4e0b\u6765\u533a\u522b\u5c31\u4e0d\u4e00\u6837\u4e86\u3002<s> \u4e0b\u9762\u8fd8\u6709\u66f4\u5feb\u7684\u65b9\u6cd5\u3002<\/s><\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u65b9\u6cd5\u4e09<\/p><\/blockquote>\n\n\n\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport java.io.BufferedReader;\nimport java.io.FileReader;\nimport java.io.IOException;\n\n\/**\n * @Author Diuut\n * @Date 2019\/8\/15  23:31\n *\/\npublic class Test0814_5_showPrime {\n    public static void main(String[] args) throws IOException {\n        long start = System.currentTimeMillis();\n        BufferedReader isr=new BufferedReader(new FileReader(&quot;Week07\/\/prime2.txt&quot;));\n        String ss;\n        while ((ss=isr.readLine())!=null) {\n            System.out.println(ss);\n        }\n        isr.close();\n        long end = System.currentTimeMillis();\n        System.out.println(&quot;\u672c\u6b21\u8fd0\u884c\u8017\u65f6\uff1a&quot;+(end-start));\n    }\n}\n<\/pre>\n\n\n\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\u672c\u6b21\u8fd0\u884c\u8017\u65f6\uff1a171<\/pre>\n\n\n\n<p>\u66f4\u5feb\u7684\u65b9\u6cd5\u6307\u7684\u5c31\u662f\u76f4\u63a5\u5c06\u9700\u8981\u7684\u6570\u636e\u6253\u5305\u5b58\u597d\uff0c\u4e0d\u7528\u6d88\u8017\u7cfb\u7edf\u8d44\u6e90\u8fdb\u884c\u8c03\u7528\u8ba1\u7b97\uff0c\u5c31\u50cf\u4e4b\u524d\u8bd5\u56fe\u4f18\u5316\u7ad9\u70b9\u8bbf\u95ee\u901f\u5ea6\u7684\u65f6\u5019\u53d1\u73b0\u7684\u4e00\u4e2a\u63d2\u4ef6 WP Super Cache \uff0c\u539f\u7406\u5c31\u662f\u5c06\u5e38\u7528\u7684\u52a8\u6001\u9875\u9762\u76f4\u63a5\u7f13\u5b58\u4e3a\u9759\u6001\u9875\u9762\uff0c\u540credis\u7f13\u5b58\u4f18\u5316\u4e00\u6837\u3002\u7528\u6237\u8bbf\u95ee\u7684\u65f6\u5019\u76f4\u63a5\u8bfb\u53d6\u7f13\u5b58\u597d\u7684\u9759\u6001\u9875\u9762\uff0c\u800c\u4e0d\u662f\u53d1\u9001\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u518d\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\uff0c\u7b80\u5355\u66b4\u529b\u6ca1\u6709\u6280\u672f\u542b\u91cf\uff0c\u4f46\u5374\u6709\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ea\u8981\u4ed4\u7ec6\u60f3\u4e00\u60f3\u5c31\u80fd\u5199\u51fa\u6765\u7684\u4ee3\u7801\uff0c\u4f46\u662f\u5f97\u51fa\u7ed3\u679c\u5bb9\u6613\uff0c\u5f97\u51fa\u7ed3\u679c\u82b1\u8d39\u7684\u65f6\u95f4\u5c31\u4e0d\u4e00\u6837\u4e86\u3002\u4e3a\u4e86\u5bf9\u6bd4\u51fa\u6548\u679c\uff0cN\u53d610000<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=492\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">\u83b7\u53d6N\u4ee5\u5185\u7684\u8d28\u6570(Prime)<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":528,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[13,15],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/492"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=492"}],"version-history":[{"count":0,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/492\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/528"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}