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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started