Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PrimeCounter {
- // Returns true if x is prime, and false otherwise.
- private static boolean isPrime(int x) {
- for (int i = 2; i <= x / 2; i++) {
- if (x % i == 0) {
- return false;
- }
- }
- return true;
- }
- // Returns the number of primes <= N.
- private static int primes(int N) {
- int counter = 1;
- for (int num = 2; counter <= N; num++) {
- if (isPrime(N)) {
- counter++;
- }
- }
- return counter;
- }
- // Entry point. [DO NOT EDIT]
- public static void main(String[] args) {
- int N = Integer.parseInt(args[0]);
- System.out.println(primes(N));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement