Exercise 752

Write a program in Python that searches for the greatest divisor strictly less than a given positive integer n. Example for n = 18 the greatest divisor of n is 9

Solution

def greatestDivisor(n):
# Initializing the gratest divisor of n
d = n - 1

# while d is not divisor of n, we decrement it
while( n%d != 0):
d = d - 1
return d

# Testing algorithm
print("The greatest divisor of 18 is : " , greatestDivisor(18))
# The output is : The greatest divisor of 18 is : 9

Younes Derfoufi
my-courses.net

Leave a Reply