A positive integer is prime if it’s divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. The number 1, by definition, is not prime. a) Write a method that determines whether a number is prime. b) Use this method in an application that determines and displays all the prime numbers less than 10,000. How many numbers up to 10,000 do you have to test to ensure that you’ve found all the primes? c) Initially, you might think that n/2 is the upper limit for which you must test to see whether a number n is prime, but you need only go as high as the square root of n. Rewrite the program, and run it both ways.

Short Answer

Expert verified
To determine if a number is prime, test divisibility from 2 up to the number's square root. Using the optimized method, all primes less than 10,000 can be found with fewer tests, leading to better performance.

Step by step solution

01

Identifying the problem for part (a)

We need to write a method to determine if a number is prime. A prime number is only divisible by 1 and itself. To check if a number is prime, we iterate from 2 to the number minus one and check if the number is divisible by any of those. If it is, the number is not prime.
02

Creating the prime-checking method for part (a)

Define a method called 'isPrime' that takes an integer 'n' as a parameter. If 'n' is less than 2, return false. Iterate from 2 to 'n-1'. For each 'i' in that range, if 'n' mod 'i' equals 0, return false. After the loop, return true, as no divisors were found.
03

Developing the application for part (b)

Use the previously defined 'isPrime' method in an application that loops from 2 to 9,999. For each number, check if it is prime using the 'isPrime' method. If it is, display the number. Keep a count of the number of prime numbers found.
04

Optimized prime-checking method for part (c)

You can optimize the 'isPrime' method by checking for divisibility only up to the square root of 'n'. Modify the method so the iteration goes from 2 to 'Math.sqrt(n)'. If 'n' mod 'i' equals 0 at any point in this range, return false. Otherwise, return true.
05

Comparing performance for part (c)

Run the application with the original 'isPrime' method and note the performance. Then, run the application with the optimized 'isPrime' method. The second execution should be faster as it performs significantly fewer divisibility checks especially as 'n' gets larger.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Java Programming
Java is a widely used programming language known for its portability, performance, and strong object-oriented features. It operates on the principle of 'Write Once, Run Anywhere', making Java code compilable and executable on any platform that has a Java Virtual Machine (JVM).

Java's syntax is similar to C++, but with a simpler object model and fewer low-level facilities. Java also eliminates the possibility of memory corruption by managing memory through an automatic garbage collector. When solving problems like determining prime numbers, Java provides a robust platform with powerful libraries like Math, which can be incredibly useful for mathematical operations and performance optimizations.
Prime Number Algorithm
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. When it comes to algorithms for identifying prime numbers, the basic approach is to iterate through all possible divisors and check for divisibility.

In Java, for example, you could implement a isPrime method that would use a loop to check every number from 2 to the input number minus one. If any number divides evenly into the input number (meaning the remainder is zero when using the modulus operator), the number is not prime. This algorithm, while straightforward, is not the most efficient way of finding prime numbers, especially as the input number grows larger.
Math.sqrt Optimization
The performance of prime checking can be greatly improved by implementing Math.sqrt optimization. Since any non-prime number must have a divisor no larger than its square root, it's unnecessary to check for divisors beyond the square root of the number being tested.

This significantly reduces the number of iterations required, as you only iterate up to the square root of the number, which can be obtained by calling Math.sqrt(n) in Java. Here's a simple enhancement to the prime number checking loop: instead of iterating from 2 to ‘n-1’, iterate from 2 to (int) Math.sqrt(n) and perform the same divisibility check. This improved method will have the same results but with a considerable reduction in computation time, especially for larger numbers.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a method isPerfect that determines whether parameter number is a perfect number. Use this method in an application that displays all the perfect numbers between 1 and 1000. Display the factors of each perfect number to confirm that the number is indeed perfect. Challenge the computing power of your computer by testing numbers much larger than 1000. Display the results.

Write statements that assign random integers to the variable n in the following ranges: a) 1 ?n ? 2. b) 1 ?n ? 100. c) 0 ?n ? 9. d) 1000 ?n ? 1112. e) –1 ?n ? 1. f) –3 ?n ? 11.

Math.floor can be used to round values to the nearest integer—e.g., y = Math.floor(x + 0.5); will round the number x to the nearest integer and assign the result to y. Write an application that reads double values and uses the preceding statement to round each of the numbers to the nearest integer. For each number processed, display both the original number and the rounded number.

Write an application that prompts the user for the radius of a circle and uses a method called circleArea to calculate the area of the circle.

Write a method qualityPoints that inputs a student’s average and returns 4 if it’s 90–100, 3 if 80–89, 2 if 70–79, 1 if 60–69 and 0 if lower than 60. Incorporate the method into an application that reads a value from the user and displays the result.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free