fork(1) download
  1. /**
  2.  * Author:- nikhil.lohia
  3.  */
  4. class Ideone {
  5.  
  6. public Ideone() {
  7. }
  8.  
  9. public static void main(String[] args) {
  10. Ideone method2 = new Ideone();
  11. // Code obtained from http://w...content-available-to-author-only...s.com
  12. // Feel free to copy but please acknowledge wherever possible
  13. method2.findPrimes(25);
  14. }
  15.  
  16. private void findPrimes(int limit) {
  17.  
  18. // Special handling for 2
  19. if (limit > 2)
  20. System.out.print("2,");
  21.  
  22. for (int i = 2; i < limit; i++) {
  23.  
  24. boolean isPrime = true;
  25.  
  26. // Utilizing our previous optimizations
  27. if (i % 2 == 0) {
  28. isPrime = false;
  29. }
  30.  
  31. // Get the square root
  32. int s = (int) Math.sqrt(i);
  33.  
  34. // We need to loop only until the square root of that number
  35. for (int j = 3; j <= s; j += 2) {
  36. if (i % j == 0) {
  37. isPrime = false;
  38. }
  39. }
  40.  
  41. if (isPrime)
  42. System.out.print(i + ",");
  43. }
  44.  
  45. }
  46. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
2,3,5,7,11,13,17,19,23,