Write a program that prompts the user for an integer value and that reports the sum of
Question:
Write a program that prompts the user for an integer value and that reports the sum of the first prime numbers, reporting a sum of 0 if the user enters a value less than 1. Structure your program to be similar to the case study, using an iterating function to produce the sequence 1, 2, 3, . . . , and filtering using the isPrime method that appears at the end of section 19.3 . Don’t include the unordered modifier because we want the first primes, not some other combination of primes. Include timing code and print how long it takes to compute the sum. Once you have a working program, explore the following efficiency improvements and note how the time changes with each using a fairly large value of such as 10,000:
- Modify isPrime to check only up to the square root, as in the case study (remember that 1 is not a prime).
- Modify the iterating function to examine only odd numbers and manually add 2 to the sum (because 2 is the only even prime).
- Modify the overall iteration to include the parallel modifier, as in the case study. Note that the benefits of parallel execution might not be evident for this computation because the problem size is fairly constrained (values of greater than 20,043 will lead to integer overflow).
Step by Step Answer:
Building Java Programs A Back To Basics Approach
ISBN: 9780135471944
5th Edition
Authors: Stuart Reges, Marty Stepp