An integer is said to be prime if it’s greater than 1 and divisible only by 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. a)  Write a function that determines whether a number is prime.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section: Chapter Questions
Problem 1PP
icon
Related questions
Question

An integer is said to be prime if it’s greater than 1 and divisible only by 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not.

  1. a)  Write a function that determines whether a number is prime.

  2. b)  Use this function in a script that determines and prints all the prime numbers between

    1 and 10,000. How many of these 10,000 numbers do you really have to test before

    being sure that you have found all the primes? Display the results in a <textarea>.

  3. c)  Initially, you might think that n/2 is the upper limit for which you must test to see whether a number is prime, but you need go only as high as the square root of n. Why? Rewrite the program using the Math.sqrt method to calculate the square root, and run

    it both ways. Estimate the performance improvement.

Please write the code in JavaScript HTML5. 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr