Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if the given number is Ore number or not in Python
An ore number (or harmonic divisor number) is a positive integer where the harmonic mean of its divisors is an integer. The harmonic mean of divisors is calculated as the number of divisors divided by the sum of reciprocals of all divisors.
For example, 28 has divisors [1, 2, 4, 7, 14, 28]. The harmonic mean is 6 ÷ (1/1 + 1/2 + 1/4 + 1/7 + 1/14 + 1/28) = 3, which is an integer.
Finding All Divisors
First, we need a function to find all divisors efficiently by checking only up to the square root ?
import math
def get_all_divisors(n):
divisors = []
for i in range(1, int(math.sqrt(n)) + 1):
if n % i == 0:
divisors.append(i)
if i != n // i: # Avoid duplicate for perfect squares
divisors.append(n // i)
return sorted(divisors)
# Test with 28
divisors = get_all_divisors(28)
print("Divisors of 28:", divisors)
Divisors of 28: [1, 2, 4, 7, 14, 28]
Calculating Harmonic Mean
The harmonic mean of divisors is: number_of_divisors ÷ sum_of_reciprocals ?
import math
def get_all_divisors(n):
divisors = []
for i in range(1, int(math.sqrt(n)) + 1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n // i)
return sorted(divisors)
def get_harmonic_mean(n):
divisors = get_all_divisors(n)
sum_of_reciprocals = sum(1/d for d in divisors)
return len(divisors) / sum_of_reciprocals
# Test with 28
harmonic_mean = get_harmonic_mean(28)
print(f"Harmonic mean of divisors of 28: {harmonic_mean}")
Harmonic mean of divisors of 28: 3.0
Complete Ore Number Checker
Now we combine everything to check if a number is an ore number ?
import math
def get_all_divisors(n):
divisors = []
for i in range(1, int(math.sqrt(n)) + 1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n // i)
return sorted(divisors)
def is_ore_number(n):
if n <= 0:
return False
divisors = get_all_divisors(n)
sum_of_reciprocals = sum(1/d for d in divisors)
harmonic_mean = len(divisors) / sum_of_reciprocals
# Check if harmonic mean is an integer
return harmonic_mean == int(harmonic_mean)
# Test with different numbers
test_numbers = [1, 6, 28, 140, 270, 496, 672, 30]
for num in test_numbers:
result = is_ore_number(num)
divisors = get_all_divisors(num)
harmonic_mean = len(divisors) / sum(1/d for d in divisors)
print(f"{num}: {result} (harmonic mean = {harmonic_mean:.3f})")
1: True (harmonic mean = 1.000) 6: True (harmonic mean = 2.000) 28: True (harmonic mean = 3.000) 140: True (harmonic mean = 5.000) 270: True (harmonic mean = 4.000) 496: True (harmonic mean = 5.000) 672: True (harmonic mean = 6.000) 30: False (harmonic mean = 2.333)
Key Properties
- All perfect numbers (like 6, 28, 496) are ore numbers
- The smallest ore numbers are: 1, 6, 28, 140, 270, 496, 672, 1638, 2970, 6200
- Ore numbers are relatively rare − there are only 44 ore numbers less than 10,000
Conclusion
An ore number has a harmonic mean of divisors that is an integer. Use the formula: harmonic_mean = count_of_divisors ÷ sum_of_reciprocals_of_divisors. The algorithm efficiently finds divisors by checking only up to the square root.
---