Exercise 751
Write a program in Python that finds the smallest divisor strictly greater than 1 of a given positive integer n. Example for n = 15 the smallest divisor of n is 3
Solution
def smallestDivisor(n):
# Initializing the smallest divisor of n
d = 2
# while d is not divisor of n, we increment it
while( n%d != 0):
d = d + 1
return d
# Testing algorithm
print("The smallest divisor of 18 is : " , smallestDivisor(18))
# The output is : The smallest divisor of 18 is : 2
Younes Derfoufi
my-courses.net
my-courses.net