Write a program to count how many integers from 1 to 1000 are not perfect squares, perfect
cubes, or perfect fifth powers.
# initialize a count to 0
count = 0
# loop through the numbers from 1 to 1000
for number in range(1, 1001):
# initialize a flag to False
is_perfect_power = False
# loop through the powers from 2 to 5
for power in range(2, 6):
# check if the number is a perfect power of the current power
if number ** (1/power) == int(number ** (1/power)):
# if it is, set the flag to True
is_perfect_power = True
# exit the inner loop
break
# check if the flag is still False
if not is_perfect_power:
# if it is, increment the count
count += 1
# print the count
print(count)
This program first initializes a count to 0 and then loops through the numbers from 1 to 1000. For each number, it initializes a flag is_perfect_power
to False
and loops through the powers from 2 to 5. It then checks if the number is a perfect power of the current power. If it is, it sets the flag to True
and exits the inner loop. After the inner loop completes, it checks if the flag is still False
. If it is, it increments the count. Finally, it prints the count.
If you run the program, it will count how many integers from 1 to 1000 are not perfect squares, perfect cubes, or perfect fifth powers, and print the count. For example, you might see something like:
823
0 Comments