java版求100以内的素数
package com.sjzmlb.test; public class TestPrimeTime { public static void main(String[] args) { long start = System.nanoTime(); // method1(); method2(); long end = System.nanoTime(); System.out.println(end - start); } public static void method1() { int i,j,k; for(i = 1;i < 101;i++){ k = (int)Math.sqrt(i); for(j = 2;j <= k;j++){ if(i % j == 0){ break; } } if(j > k){ System.out.println(i); } } } public static void method2() { int i,j; int[] a = new int[101]; for(i = 1;i < 101;i++){ a[i] = 1; } for(i = 2;i < 101;i++){ if(a[i] != 0){ for(j = i + i;j < 101;){ if(j % i == 0){ a[j] = 0; } j = j + i; } } } for(i = 2;i < 101;i++){ if(a[i] != 0){ System.out.println(i); } } } }