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

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 Solution

3.25 Rating (166 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Approach Naive Approach Iterate the loop from 1 to r and add all the numbers which ... View full answer

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 Building Java Programs A Back to Basics Approach Questions!