fork(4) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. findPrimes(25);
  14. }
  15.  
  16. static void findPrimes(int limit) {
  17.  
  18. // Special handling for 2
  19. if (limit > 2)
  20. System.out.print("2,");
  21.  
  22. // Loop from the first Prime till limit
  23. for (int i = 2; i < limit; i++) {
  24. // Assume number to be prime
  25. boolean isPrime = true;
  26.  
  27. if (i % 2 == 0)
  28. isPrime = false;
  29. else {
  30.  
  31. // Loop from 3 to number/2
  32. for (int j = 3; j < i / 2; j += 2) {
  33.  
  34. // If the number divides, it means the number is not prime
  35. if (i % j == 0) {
  36. isPrime = false;
  37. break;
  38. }
  39. }
  40. }
  41.  
  42. // Print the number if it is prime
  43. if (isPrime)
  44. System.out.print(i + ",");
  45. }
  46. }
  47. }
Success #stdin #stdout 0.09s 320320KB
stdin
Standard input is empty
stdout
2,3,5,7,11,13,17,19,23,