When it is required to find out if a number is a prime number or not using recursion technique, a method is defined, and the ‘while’ condition is used.
The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.
Below is a demonstration for the same −
def check_prime(my_num, my_val = None): if my_val is None: my_val = my_num – 1 while my_val >= 2: if my_num % my_val == 0: print(“The number is not a prime number”) return False else: return check_prime(my_num, my_val-1) else: print(“The number is a prime number”) return ‘True’ my_num = int(input(“Enter the number that you wish to examine : “)) print(“The number is being checked…”) check_prime(my_num)
Enter the number that you wish to examine : 46 The number is being checked… The number is not a prime number