Question: Problem 1. (Counting Primes) Implement the static method isPrime() in PrimeCounter.java that takes an integer argument xand returns true if it is prime and false

Problem 1. (Counting Primes) Implement the static method isPrime() in PrimeCounter.java that takes an integer argument xand returns true if it is prime and false otherwise. Also implement the static method primes() that takes an integer argumentN and returns the number of primes less than or equal to N. Recall that a number x is prime if it is not divisible by any number i [2, x].

$ javac PrimeCounter.java

$ java PrimeCounter 100 25

$ java PrimeCounter 1000000 78498

// PrimeCounter.java: takes an integer N as a command-line argument and writes // the number of primes <= N.

import edu.princeton.cs.algs4.StdOut;

public class PrimeCounter { // Returns true if x is prime, and false otherwise. private static boolean isPrime(int x) { ... }

// Returns the number of primes <= N. private static int primes(int N) { ... }

// Entry point. [DO NOT EDIT] public static void main(String[] args) { int N = Integer.parseInt(args[0]); StdOut.println(primes(N)); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!