Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In this activity, you are going to be thinking about how to test the isPrime function which takes a number and tests if it is prime. For each question, we will describe a mistake that the programmer might have made in writing isPrime. We will imagine that there is a check function that takes the number with which to test isPrime, and the expected answer (True or False). The check function might look something like this:

Python
def check(num, expected):
    if isPrime(num) != expected:
        print("isPrime("+str(num)+") did not work")
        pass
    pass


For each question, your answer should be a single call to check that identifies the problem described in the question prompt. For example, if the question prompt suggests that isPrime(3) does not work correctly, you could answer

Python
check(3, True)


to identify the mistake.

Question 1
Suppose the programmer writing isPrime mistakenly thought that all prime numbers were odd, so they first checked if the number passed in is even, and if so, returned False right away.

Question 2
Suppose the programmer could not think of a general algorithm so instead they just checked several specific case with the first several prime numbers:
Python
if (num == 2):
    return True
if (num == 3):
   return True
# several more cases
return False


What I have tried:

I am trying but not getting the correct answer please help.
Posted
Updated 8-Dec-23 3:26am
v2
Comments
Richard Deeming 8-Dec-23 9:26am    
Nobody is going to do your homework for you. Unless you show us what you have tried, and explain what the problem is and where you are stuck, nobody can help you.
Richard MacCutchan 8-Dec-23 9:29am    
Question 1 is simplicity itself, just check if the number is odd or not. Question 2 requires you to implement a general rule for prime numbers. A quick search on Google will get you some good suggestions.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900