Exercise 201

Write an algorithm as a python function that takes as parameters an integer n and which returns the list of divisors d whose last digit is equal to 1. Test your algorithm for n = 727821.

Solution


def listDiv(n):
# initializing searched list
listDiv =[]
# Browsing through all integers i = 1 2 3 ... n
for i in range(1, n+1):
# the last dijit of i is equal to i%10, see the previous exercise 72
if( n%i == 0 and i%10 == 1):
listDiv.append(i)
return listDiv
# Testing algorithm
print("The list of divisors of 727821 whose last digit is equal to 1 is L = ", listDiv(727821))


Younes Derfoufi
my-courses.net

Leave a Reply