Python Program to Print Powerful Numbers In An Interval
A powerful number is a positive integer number such that every prime factor and their square divides a number.
Examples of powerful numbers are: 25, 27, 32, 36, 49, 64, 72, 81, 100, 108, 121, 125, 128, 144, 169, 196, ...
This Python program generates (prints) all powerful numbers in an interval given by users.
Python Source Code: Generate Powerful Numbers in Range
# Function definition to get all prime factors
def get_prime_factors(n):
i = 2
prime_factors = []
while i*i <= n:
if n%i == 0:
prime_factors.append(i)
n //= i
else:
i += 1
if n>1:
prime_factors.append(n)
return prime_factors
# Function definition to check powerful number
def is_powerful(n):
# get prime factors
prime_factors = get_prime_factors(n)
# filter to get unique prime factors
unique_prime_factors = tuple(dict.fromkeys(prime_factors))
for p in unique_prime_factors:
if n % p != 0 or n % (p*p) != 0:
return False
return True
# Reading minimum and maximum number
min_num = int(input('Enter lower range: '))
max_num = int(input('Enter upper range: '))
print('All powerful numbers from %d to %d are:' %(min_num, max_num))
for i in range(min_num, max_num+1):
if is_powerful(i):
print(i, end=' ')
Output
Enter lower range: 1 Enter upper range: 1000 All powerful numbers from 1 to 1000 are: 1 4 8 9 16 25 27 32 36 49 64 72 81 100 108 121 125 128 144 169 196 200 216 225 243 256 288 289 324 343 361 392 400 432 441 484 500 512 529 576 625 648 675 676 729 784 800 841 864 900 961 968 972 1000