Please do it in Python
Write the simplest program that will demonstrate iteration vs recursion using the following guidelines -
Grading:
I understand most of this information is more suitable to C++ but our instructor wants us to modify it to do it in Python. As long as you fufill the parameters the best you can in Python and works that all I want. Thank you
Program:
# function is used to check whether a given number or not using iteration
def isPrime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False;
return True
# function is used to check whether a given list has all prime numbers or not using iteration
def check_list_prime_iteration(arr,le):
count=0
for i in arr:
if(isPrime(i)):
count=count+1
if(count==le):
return True
else:
return False
# function is used to check whether a given list has all prime numbers or not using recusrion
def check_list_prime_recursion(arr,le):
count=0
for i in arr:
if(isPrime_Rec(i)):
count=count+1
if(count==le):
return True
else:
return False
# function is used to check whether a given number or not using recusrion
def isPrime_Rec(n, i = 2):
if (n <= 2):
return True if(n == 2) else False
if (n % i == 0):
return False
if (i * i > n):
return True
return isPrime_Rec(n, i + 1)
# List to store values
l=[]
i=1
# input taking
n=int(input("Enter size of an Array"))
while(i<=n):
input_value=int(input("Enter number"))
if(input_value>=1 and input_value<=99):
l.append(input_value)
else:
print("please enter valid input between 1 and 99")
i=i-1
i=i+1
# Final list after validation
print("Array List",l)
if check_list_prime_iteration(l,len(l)):
print("Prime Array using iteration")
else:
print("Not a Prime Array using iteration")
if check_list_prime_recursion(l,len(l)):
print("Prime Array using recursion")
else:
print("Not a Prime Array using recursion")
Output:
Get Answers For Free
Most questions answered within 1 hours.