zoukankan      html  css  js  c++  java
  • 求质数算法N

    参考博文http://blog.csdn.net/program_think/article/details/7032600

    求小于N的所有质数

    1.尝试所有小于N的数

    2.只要尝试小于√x 的质数即可。而这些质数,恰好前面已经算出来了

    3.筛选法 首先,2是公认最小的质数,所以,先把所有2的倍数去掉;然后剩下的那些大于2的数里面,最小的是3,所以3也是质数;然后把所有3的倍数都去掉,剩下的那些大于3的数里面,最小的是5,所以5也是质数......

     1 public class TestSushu {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7 
     8         TestSushu test = new TestSushu();
     9 
    10         ArrayList<Integer> mlist = test.getNumforSelect(10);
    11 
    12         System.out.println(mlist.toString());
    13     }
    14 
    15     // 求小于N的所有素数
    16 
    17     // 1) 试除法 -狂举法
    18 
    19     // 2)试除法--算法
    20     // 只要尝试小于√x 的质数即可
    21 
    22     // 3)筛法
    23 
    24     public ArrayList<Integer> getNumforDivAll(int N) {
    25         ArrayList<Integer> mList = new ArrayList<Integer>();
    26         if (N < 2) {
    27         } else {
    28             for (int i = 2; i <= N; i++) {
    29                 boolean flag = false;
    30                 for (int j = 2; j < i; j++) {
    31                     if (i % j == 0) {// 有其他余数 则不是质数
    32                         flag = true;
    33                         break;
    34                     }
    35                 }
    36                 if (!flag) {
    37                     mList.add(new Integer(i));
    38                 }
    39             }
    40         }
    41 
    42         return mList;
    43     }
    44 
    45     public ArrayList<Integer> getNumforDivOpt(int N) {
    46         ArrayList<Integer> mList = new ArrayList<Integer>();
    47         if (N < 2) {
    48 
    49         } else {
    50             int square = (int) Math.sqrt(N);
    51 
    52             for (int i = 2; i <= N; i++) {
    53                 boolean flag = false;
    54                 for (int j : mList) {
    55                     if (i % j == 0) {// 有其他余数 则不是质数
    56                         flag = true;
    57                         break;
    58                     } else if (j > square) {// 超过平方根 不再判断
    59                         break;
    60                     }
    61                 }
    62                 if (!flag) {
    63                     mList.add(new Integer(i));
    64                 }
    65             }
    66         }
    67 
    68         return mList;
    69     }
    70 
    71     // 筛选法
    72     public ArrayList<Integer> getNumforSelect(int N) {
    73         ArrayList<Integer> mList = new ArrayList<Integer>();
    74         for (int i = 2; i < N; i++) {
    75             mList.add(new Integer(i));
    76         }
    77 
    78         int idx = 0;
    79         while (idx < mList.size()) {
    80             int curdata = mList.get(idx);
    81             int i = idx+1;
    82             while (i<mList.size()) {
    83                 if(mList.get(i)%curdata==0){// 整除的倍数 删掉
    84                     mList.remove(i);
    85                 }else{
    86                     i++;
    87                 }
    88             }
    89             idx++;
    90         }
    91         
    92         return mList;
    93     }
    94 
    95 }
  • 相关阅读:
    反射
    IDEA配置数据库
    配置idea的maven镜像为aliyun
    蓝桥---芯片测试(思维)
    汉诺塔(思维、DP思想)
    立方数(质因子、优化)
    碎碎念(DP)
    牛牛战队的比赛地(三分)
    子段乘积(尺取、逆元)
    子段异或(位运算)
  • 原文地址:https://www.cnblogs.com/wjw334/p/zhishu.html
Copyright © 2011-2022 走看看