1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | import java.util.Date; public class NumberTest4 { static int max = 1000000; static int pc = 0; static double[] primesd = new double[max]; static long[] primesl = new long[max]; public static void main(String[] args) { long time = 0; long c = 5; double cd = c; double sqcd = Math.ceil(Math.sqrt(cd))+1; put(2.0d); put(3.0d); time = new Date().getTime(); while(pc<max){ boolean ip = true; for(int i=0; i<pc;i++){ if(primesd[i]<sqcd){ if(cd/primesd[i] == 0){ ip = false; break; } }else{ break; } } if(ip) put(cd); c+=2; cd = c; sqcd = Math.ceil(Math.sqrt(cd))+1; } System.out.println(new Date().getTime() - time); pc = 0; c = 5; long sqcl = (long)Math.ceil(Math.sqrt(cd))+1; put(2l); put(3l); time = new Date().getTime(); while(pc<max){ boolean ip = true; for(int i=0; i<pc;i++){ if(primesl[i]<sqcl){ if(cd/primesl[i] == 0){ ip = false; break; } }else{ break; } } if(ip) put(c); c+=2; sqcl = (long)Math.ceil(Math.sqrt(cd))+1; } System.out.println(new Date().getTime() - time); System.out.println("F"); } static public void put(double d){ primesd[pc++] = d; } static public void put(long d){ primesl[pc++] = d; } } |
Direct link: https://paste.plurk.com/show/2449668