Describe a recursive algorithm that takes a list L of distinct integers and find
ID: 3688872 • Letter: D
Question
Describe a recursive algorithm that takes a list L of distinct integers and finds the number of primes the fist. Write your answer in pseudo-code or any well known procedural language like Python, Java. C++,... Assume that you have the following three functions that operate on lists. "empty?" which returns TRUE if a given list is empty, FALSE otherwise "first" which returns the first element of a given nonempty list. "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
#include<stdio.h>
int isPrime(int,int);
int main(){
int num,pr,n,i,a[100][2];
printf("size of list");
scanf("%d",&n);
printf("Enter list: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i][0]);
}
printf("arry is");
for(i=0;i<n;i++)
printf(" %d",a[i][0] );
for(i=0;i<n;i++)
{
a[i][1] = isPrime(a[i][0],a[i][0]/2);
}
printf("enter 1.empty? 2.return 1st ele 3.rest");
scanf("%d",&pr);
switch(pr)
{
case 1:if(n>0)
printf("FALSE");
else
printf("true");
break;
case 2: if(n>0)
printf("%d",a[0][0]);
break;
case 3: if(n>0)
{if(n==1)
printf("empty list");
for(i=1;i<0;i++)
printf("%d ",a[i][0]);
}
break;
}
printf("prime numbers in arry are: ");
for(i=0;i<n;i++)
{
if(a[i][1]==1)
printf(" %d",a[i][0]);
}
// if(prime==1)
// printf("%d is a prime number",num);
// else
// printf("%d is not a prime number",num);
return 0;
}
int isPrime(int num,int i){
if(i==1){
return 1;
}else{
if(num%i==0)
return 0;
else
isPrime(num,i-1);
}
}