求素数方法

来源:互联网 发布:java自定义异常类 编辑:程序博客网 时间:2024/04/20 20:23
  1. public class Prime {
  2.     /*
  3.      * 
  4.      * 一般求素数方法
  5.      * 
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         for (int i = 2; i < 100; i++) {
  10.             int j;
  11.             for (j = 2; j < (int) (Math.sqrt(i) + 1); j++) {
  12.                 if (i % j == 0) {
  13.                     break;
  14.                 }
  15.             }
  16.             if (j > (int) Math.sqrt(i)) {
  17.                 System.out.print(i + " ");
  18.             }
  19.         }
  20.     }}




  1.    1public class Prime {
  2.    2.     /*
  3.    3.      * 
  4.    4.      * 一般求素数方法
  5.    5.      * 
  6.    6.      * @param args
  7.    7.      */
  8.    8.
  9.    9.     public static void main(String args[]) 
  10.   10.     { 
  11.   11.     int i,j; 
  12.   12.     for(j=2;j<=100;j++) //2-100的数 
  13.   13.     { 
  14.   14.     for(i=2;i<=j/2;i++) //
  15.   15.         { 
  16.   16.     if(j%i==0
  17.   17.     break
  18.   18.     } 
  19.   19.     if(i>j/2//
  20.   20.     { 
  21.   21.     System.out.print(" "+j); 
  22.   22.     } 
  23.   23.     } 
  24.   24.     } 
  25.   25.     
  26.   26.
  27.   27.     
  28.   28. }

原创粉丝点击