An integer is called squarefree if it is not divisible by any perfect squares other than 1. For
instance, 42 is squarefree because its divisors are 1, 2, 3, 6, 7, 21, and 42, and none of those
numbers (except 1) is a perfect square. On the other hand, 45 is not squarefree because it is
divisible by 9, which is a perfect square. Write a program that asks the user for an integer and
tells them if it is squarefree or not.
# get the number from the user
number = int(input("Enter an integer: "))
# loop through the numbers from 2 to the square root of the number
for i in range(2, int(number ** 0.5)+1):
# check if the number is divisible by i^2
if number % (i**2) == 0:
# if it is, the number is not squarefree
print(f"{number} is not squarefree")
# exit the program
exit()
# if the loop completes, the number is squarefree
print(f"{number} is squarefree")
This program first gets the number from the user and then loops through the numbers from 2 to the square root of the number. For each number, it checks if the given number is divisible by the square of the number. If it is, the number is not squarefree and the program prints an appropriate message and exits. If the loop completes, the number is squarefree and the program prints an appropriate message.
If you run the program, it will ask you to enter an integer, and then it will tell you if it is squarefree or not. For example, if you enter 42
when prompted, you might see something like:
Enter an integer: 42
42 is squarefree
0 Comments