Question: 1 . boolean isPrime ( int n ) { / / tests if n is a prime number 2 / / Input: a positive integer

1. boolean isPrime(int n){// tests if n is a prime number
2// Input: a positive integer n
3.// Output: returns true if n is a prime number, false otherwise
4. for (int x =2; x*x <= n; x++){
5. if (n % x ==0){// find that x divides n
6. return false;
7.}
8.}
9. return true; // found no divisor of n
10.}
Note: when giving the big-Oh, give the nearest possible upper limit.
possible. For example, if you can prove that f(n) is O(n), and f(n) is O(n2),
choose the nearest upper bound, i.e. that f(n) is O(n).
(a)(5 pts) Give a big-Oh for T(n), the worst-case execution time of this algorithm for an integer input n.
algorithm for an integer input n. Explain how you obtained this worst-case
case.
(b)(5 pts) Give a big-Oh for B(n), the best execution time for this algorithm
for an integer input of input n. Explain the type of inputs (not just one
but an infinite sequence of inputs) that will give this best case.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Programming Questions!