74.1k views
5 votes
A number is considered a good number when it is both divisible by a prime number and divisible by the square of that prime number. Write a program to list such good numbers in the interval between two given positive integers.

1 Answer

3 votes

Final answer:

Here is a program in Python that can do this.

def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True

def list_good_numbers(start, end):
good_numbers = []
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
for num in range(start, end + 1):
for prime in primes:
if num % (prime**2) == 0:
good_numbers.append(num)
break
return good_numbers

start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))

good_numbers = list_good_numbers(start, end)
print("Good numbers in the interval:", good_numbers)

Step-by-step explanation:

To find the good numbers in an interval, we need to check if each number in the interval is divisible by a prime number and divisible by the square of that prime number.

Here is a program in Python that can list the good numbers:

def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True

def list_good_numbers(start, end):
good_numbers = []
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
for num in range(start, end + 1):
for prime in primes:
if num % (prime**2) == 0:
good_numbers.append(num)
break
return good_numbers

start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))

good_numbers = list_good_numbers(start, end)
print("Good numbers in the interval:", good_numbers).
User Andrew Coleson
by
8.2k points

No related questions found