标签:
package com.android; public class SpiralPrimes { public static void main(String args[]) { long numPrimes = 0; long numAll = 0; long oneStart = 1; for (long i = 3; i < Long.MAX_VALUE; i = i + 2) { numAll = 2*i-1; if(isPrimes(i*i)){ numPrimes++; } if(isPrimes(i*i-i+1)){ numPrimes++; } if(isPrimes(i*i-2*i+2)){ numPrimes++; } if(isPrimes(i*i-3*i+3)){ numPrimes++; } if((float)numPrimes/numAll<=0.1){ System.out.println("i="+i); break; } } } public static boolean isPrimes(long n) { boolean isPrime = true; long s = (long) Math.sqrt(n); for (long i = s; i > 1; i--) { if (n % i == 0) { isPrime = false; } } return isPrime; } }
标签:
原文地址:http://www.cnblogs.com/comkingfly/p/4806809.html