fork download
  1. class PrimeTest {
  2.  
  3. /**
  4. * @param args
  5. */
  6. public static void main(String[] args) {
  7. // TODO Auto-generated method stub
  8. printPrime(1000);
  9. }
  10.  
  11. public static boolean isPrime(int n){
  12. if(n<=3)
  13. return true;
  14.  
  15. if(n%2 ==0){
  16. return false;
  17. }
  18. else{
  19. for(int i=4;i<Math.sqrt(n);i++){
  20. if(n%i==0)
  21. return false;
  22. }
  23. }
  24. return true;
  25. }
  26.  
  27. public static void printPrime(int n) {
  28. int k = 0;
  29. for(int i=1;i<=n;i++){
  30. if(isPrime(i)==true){
  31. System.out.print(i+",");
  32. k++;
  33. System.out.print(k%10==0?"\n":"");
  34. }
  35.  
  36.  
  37. }
  38. }
  39.  
  40. }
  41.  
Success #stdin #stdout 0.09s 380160KB
stdin
Standard input is empty
stdout
1,2,3,5,7,9,11,13,15,17,
19,21,23,25,27,29,31,33,37,39,
41,43,47,49,51,53,57,59,61,67,
69,71,73,79,81,83,87,89,93,97,
101,103,107,109,111,113,121,123,127,129,
131,137,139,141,149,151,157,159,163,167,
169,173,177,179,181,183,191,193,197,199,
201,211,213,219,223,227,229,233,237,239,
241,249,251,257,263,267,269,271,277,281,
283,289,291,293,303,307,309,311,313,317,
321,327,331,337,339,347,349,353,359,361,
367,373,379,381,383,389,393,397,401,409,
411,417,419,421,431,433,439,443,447,449,
453,457,461,463,467,471,479,487,489,491,
499,501,503,509,519,521,523,529,537,541,
543,547,557,563,569,571,573,577,579,587,
591,593,597,599,601,607,613,617,619,631,
633,641,643,647,653,659,661,669,673,677,
681,683,687,691,699,701,709,717,719,723,
727,733,739,743,751,753,757,761,769,771,
773,787,789,797,807,809,811,813,821,823,
827,829,831,839,841,843,849,853,857,859,
863,877,879,881,883,887,907,911,919,921,
929,933,937,939,941,947,951,953,961,967,
971,977,983,991,993,997,