Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. public class PrimeCounter {
  2. // Returns true if x is prime, and false otherwise.
  3. private static boolean isPrime(int x) {
  4. for (int i = 2; i <= x / 2; i++) {
  5. if (x % i == 0) {
  6. return false;
  7. }
  8. }
  9. return true;
  10. }
  11.  
  12. // Returns the number of primes <= N.
  13. private static int primes(int N) {
  14. int counter = 1;
  15. for (int num = 2; counter <= N; num++) {
  16. if (isPrime(N)) {
  17. counter++;
  18. }
  19. }
  20. return counter;
  21. }
  22.  
  23. // Entry point. [DO NOT EDIT]
  24. public static void main(String[] args) {
  25. int N = Integer.parseInt(args[0]);
  26. System.out.println(primes(N));
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement