Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Describe a recursive algorithm that takes a list L of distinct integers and dete

ID: 3811896 • Letter: D

Question

Describe a recursive algorithm that takes a list L of distinct integers and determines whether they are all prime. Write your answer in pseudo-code or any well-known procedural language like Python, Java, C++, You may assume that your language has a built in way to determine whether a number is prime or not, and has the following built in functions to manipulate lists. i) "empty?" which returns TRUE if a given list is empty, FALSE otherwise ii) "First" which returns the First element of a given nonempty list. iii) "rest" which returns a list containing all but the first element of a given nonempty list. Note that if the list has only one element, "rest" will return the empty list.

Explanation / Answer

procedure all_prime(L):

if empty(L) return True

return isPrime(first(L)) and all_prime(rest(L))

So this is the short recrcive procedure, isPrime will return True if number if prime

so here this is how it is working, An empty list is considered as all_prime list.

Now check if first number if prime if yes then heck if rest of list if prime recursively using our procedure.

If there is any non prime number then function return false, otherwise if all are prime function return true.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote