Check if the given number is Ore number or not
Last Updated :
01 Aug, 2022
Given a positive integer n, check if it is an Ore number or not. Print 'YES' if n is an ore number otherwise print 'NO'.
Ore Number: In mathematics, Ore numbers are positive integers whose divisors have an integer harmonic value. Ore numbers are often called harmonic divisor numbers. Ore numbers are named after Øystein Ore.
For example, 6 has four divisors namely 1, 2, 3, and 6.
The harmonic mean of the divisors is-

The harmonic mean of divisors of 6 is 2, an integer. So, 6 is an Ore number or harmonic divisor number.
First, a few Ore numbers or harmonic divisor numbers are:
1, 6, 28, 140, 270, 496, 672, 1638, 2970, 6200, 8128, 8190
Examples:
Input : N = 6
Output : Yes
Input : N = 4
Output: No
Explanation : Harmonic mean of divisors of 4
is not an Integer.

Prerequisite:
The idea is to generate all divisors of the given number and then check if the harmonic mean of the divisor is an Integer or not.
- Generate All Divisors of the given number - 'n'
- Calculate the Harmonic mean of the divisors of n
- Check if the Harmonic mean is an Integer or not
- If Yes, Then the number is an Ore Number otherwise Not
Below is the implementation of the above approach:
C++
// CPP program to check if the given number is
// Ore number
#include <bits/stdc++.h>
using namespace std;
vector<int> arr;
// Function that returns harmonic mean
void generateDivisors(int n)
{
// Note that this loop runs till square root
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
// If divisors are equal, store 'i'
if (n / i == i)
arr.push_back(i);
else // Otherwise store 'i' and 'n/i' both
{
arr.push_back(i);
arr.push_back(n / i);
}
}
}
}
// Utility function to calculate harmonic
// mean of the divisors
double harmonicMean(int n)
{
generateDivisors(n);
// Declare sum variables and initialize
// with zero.
double sum = 0.0;
int len = arr.size();
// calculate denominator
for (int i = 0; i < len; i++)
sum = sum + double(n / arr[i]);
sum = double(sum / n);
// Calculate harmonic mean and return
return double(arr.size() / sum);
}
// Function to check if a number is ore number
bool isOreNumber(int n)
{
// Calculate Harmonic mean of divisors of n
double mean = harmonicMean(n);
// Check if harmonic mean is an integer or not
if (mean - int(mean) == 0)
return true;
else
return false;
}
// Driver Code
int main()
{
int n = 28;
if (isOreNumber(n))
cout << "YES";
else
cout << "NO";
return 0;
}
Java
// Java program to check if the given
// number is Ore number
import java.util.*;
class GFG {
static Vector<Integer> arr = new Vector<Integer>();
// Function that returns harmonic mean.
static void generateDivisors(int n)
{
// Note that this loop runs till square root
for (int i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
// If divisors are equal, store 'i'
if (n / i == i)
arr.add(i);
else // Otherwise store 'i' and 'n/i' both
{
arr.add(i);
arr.add(n / i);
}
}
}
}
// Utility function to calculate harmonic mean
// of the divisors
static double harmonicMean(int n)
{
generateDivisors(n);
// Declare sum variables and initialize
// with zero.
double sum = 0.0;
int len = arr.size();
// calculate denominator
for (int i = 0; i < len; i++)
sum = sum + n / arr.get(i);
sum = sum / n;
// Calculate harmonic mean and return
return arr.size() / sum;
}
// Function to check if a number
// is Ore number
static boolean isOreNumber(int n)
{
// Calculate Harmonic mean of divisors of n
double mean = harmonicMean(n);
// Check if Harmonic mean is an Integer or not
if (mean - Math.floor(mean) == 0)
return true;
else
return false;
}
// Driver Code
public static void main(String[] args)
{
int n = 28;
if (isOreNumber(n))
System.out.println("YES");
else
System.out.println("NO");
}
}
Python3
# Python3 program to check if the
# given number is Ore number
arr = []
# Function that returns harmonic mean
def generateDivisors(n):
# Note that this loop runs till square root
for i in range(1, int(n**(0.5)) + 1):
if n % i == 0:
# If divisors are equal, store 'i'
if n // i == i:
arr.append(i)
# Otherwise store 'i' and 'n/i' both
else:
arr.append(i)
arr.append(n // i)
# Utility function to calculate harmonic
# mean of the divisors
def harmonicMean(n):
generateDivisors(n)
# Declare sum variables and initialize
# with zero.
Sum = 0
length = len(arr)
# calculate denominator
for i in range(0, length):
Sum = Sum + (n / arr[i])
Sum = Sum / n
# Calculate harmonic mean and return
return length / Sum
# Function to check if a number
# is ore number
def isOreNumber(n):
# Calculate Harmonic mean of
# divisors of n
mean = harmonicMean(n)
# Check if harmonic mean is an
# integer or not
if mean - int(mean) == 0:
return True
else:
return False
# Driver Code
if __name__ == "__main__":
n = 28
if isOreNumber(n) == True:
print("YES")
else:
print("NO")
# This code is contributed
# by Rituraj Jain
C#
// C# program to check if the given
// number is Ore number
using System;
using System.Collections;
class GFG
{
static ArrayList arr = new ArrayList();
// Function that returns harmonic mean.
static void generateDivisors(int n)
{
// Note that this loop runs
// till square root
for (int i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
// If divisors are equal,
// store 'i'
if (n / i == i)
arr.Add(i);
else // Otherwise store 'i'
// and 'n/i' both
{
arr.Add(i);
arr.Add(n / i);
}
}
}
}
// Utility function to calculate
// harmonic mean of the divisors
static double harmonicMean(int n)
{
generateDivisors(n);
// Declare sum variables and
// initialize with zero.
double sum = 0.0;
int len = arr.Count;
// calculate denominator
for (int i = 0; i < len; i++)
sum = sum + n / (int)arr[i];
sum = sum / n;
// Calculate harmonic mean
// and return
return arr.Count / sum;
}
// Function to check if a number
// is Ore number
static bool isOreNumber(int n)
{
// Calculate Harmonic mean of
// divisors of n
double mean = harmonicMean(n);
// Check if Harmonic mean is
// an Integer or not
if (mean - Math.Floor(mean) == 0)
return true;
else
return false;
}
// Driver Code
public static void Main()
{
int n = 28;
if (isOreNumber(n))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
// This code is contributed by mits
JavaScript
<script>
// Javascript program to check
// if the given number is
// Ore number
var arr = [];
// Function that returns harmonic mean
function generateDivisors(n)
{
// Note that this loop runs till square root
for (var i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
// If divisors are equal, store 'i'
if (n / i == i)
arr.push(i);
else // Otherwise store 'i' and 'n/i' both
{
arr.push(i);
arr.push(n / i);
}
}
}
}
// Utility function to calculate harmonic
// mean of the divisors
function harmonicMean(n)
{
generateDivisors(n);
// Declare sum variables and initialize
// with zero.
var sum = 0.0;
var len = arr.length;
// calculate denominator
for (var i = 0; i < len; i++)
sum = sum + (n / arr[i]);
sum = (sum / n);
// Calculate harmonic mean and return
return (arr.length / sum);
}
// Function to check if a number is ore number
function isOreNumber(n)
{
// Calculate Harmonic mean of divisors of n
var mean = harmonicMean(n);
// Check if harmonic mean is an integer or not
if (mean - parseInt(mean) == 0)
return true;
else
return false;
}
// Driver Code
var n = 28;
if (isOreNumber(n))
document.write( "YES");
else
document.write( "NO");
</script>
Time Complexity: O(sqrt(n)), Where n is the given number.
Auxiliary Space: O(sqrt(n)), for storing the divisor of n in the array
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms
DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph
Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Data Structures Tutorial
Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Binary Search Algorithm - Iterative and Recursive Implementation
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all
Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort
Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read