Number which has the maximum number of distinct prime factors in the range M to N in Python

In this article, we have a task to find and print the number which has the maximum amount of distinct prime factors between the given range M and N in Python. If there are multiple numbers falling in that criteria then print the smallest one. Let there be two numbers M and N given as input by the user. Now, we have to find the smallest number in the range M and N that has the maximum number of distinct prime factors.
A number is prime when it is divisible by 1 and itself only eg, 3,5,7,11,13 and so on. Note: 1 is not a prime number and 2 is the only even prime number. No even numbers are prime.

Now for this task, I have divided the code into three functions for better understanding and simplicity:

  • prime– This function checks whether a given number is prime or not.
#function to check if the number is prime or not
def prime(x):
    c=0
    for i in range(1,x):
        if x%i==0:
            c+=1
    if c==1:
        return True #returns True if prime
    return False #return False if composite

Let’s call the function for distinct values-

print(f"{5} is prime?",prime(5))
print(f"{6} is prime?",prime(6))
print(f"{1} is prime?",prime(1))
print(f"{2} is prime?",prime(2))

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *