Exercise 211
Write a Python program that returns the set of perfect square integers less than or equal to 100.
Solution
# function that tests if a number is a perfect square
def perfectSquare(n):
test=False
for i in range(0 , n+1):
# test if n is perfect square and change test to True
if i**2 == n:
test=True
return test
# initialization of the requested set
A = set({})
# iterate over integers from 1 to 100
for i in range(0, 101):
# test if 'i' is a perfect square
if perfectSquare(i):
A.add(i)
# display the set of perfect squares
print("A = ", A)
# output: A = {0, 1, 64, 4, 36, 100, 9, 16, 49, 81, 25}
Younes Derfoufi
my-courses.net
my-courses.net