
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if the given number is Ore number or not in Python
Suppose we have a number n. We have to check whether n is an ore number or not. As we know an ore number is a number whose divisors have an integer harmonic value.
So, if the input is like 28, then the output will be True as there are six divisors of 28: [1, 2, 4, 7, 14, 28], so
As 3 is an integer so 28 is an ore number.
To solve this, we will follow these steps −
- Define a function get_all_div() . This will take n
- div := a new list
- for i in range 1 to integer part of (square root of n), do
- if n is divisible by i, then
- if quotient of (n / i) is i, then
- insert i at the end of div
- otherwise,
- insert i at the end of div
- insert quotient of (n / i) at the end of div
- if quotient of (n / i) is i, then
- if n is divisible by i, then
- return div
- Define a function get_harmonic_mean() . This will take n
- div := get_all_div(n)
- total := 0
- length := size of div
- for i in range 0 to length - 1, do
- total := total + (n / div[i])
- total := total / n
- return length / total
- From the main method do the following:
- mean := get_harmonic_mean(n)
- if mean is an integer, then
- return True
- return False
Let us see the following implementation to get better understanding −
Example Code
def get_all_div(n): div = [] for i in range(1, int(n**(0.5)) + 1): if n % i == 0: if n // i == i: div.append(i) else: div.append(i) div.append(n // i) return div def get_harmonic_mean(n): div = get_all_div(n) total = 0 length = len(div) for i in range(0, length): total += (n / div[i]) total /= n return length / total def solve(n): mean = get_harmonic_mean(n) if mean - int(mean) == 0: return True return False n = 28 print(solve(n))
Input
28
Output
True
- Related Articles
- Check if given number is Emirp Number or not in Python
- Check whether the given number is Euclid Number or not in Python
- Swift Program to Check if the given number is Perfect number or not
- Check if a number is an Achilles number or not in Python
- Program to check whether given number is Narcissistic number or not in Python
- Check if a given number is sparse or not in C++
- Check whether the given number is Wagstaff prime or not in Python
- Check if a number is in given base or not in C++
- Check if number is palindrome or not in Octal in Python
- Check if a number is Primorial Prime or not in Python
- Python program to check if a number is Prime or not
- Program to check whether the given number is Buzz Number or not in C++
- Check whether a given number is Polydivisible or Not
- Check if a number is a Krishnamurthy Number or not in C++
- Check if a number is an Unusual Number or not in C++

Advertisements