# Harmonic divisor numbers

Consider the $$\4\$$ divisors of $$\6\$$: $$\1, 2, 3, 6\$$. We can calculate the harmonic mean of these numbers as

$$\frac 4 {\frac 1 1 + \frac 1 2 + \frac 1 3 + \frac 1 6} = \frac 4 {\frac {12} 6} = \frac 4 2 = 2$$

However, if we take the $$\6\$$ divisors of $$\12\$$ ($$\1, 2, 3, 4, 6, 12\$$) and calculate their harmonic mean, we get

$$\frac 6 {\frac 1 1 + \frac 1 2 + \frac 1 3 + \frac 1 4 + \frac 1 6 + \frac 1 {12}} = \frac 6 {\frac {28} {12}} = \frac {18} {7}$$

Ore numbers or harmonic divisor numbers are positive integers $$\n\$$ where the harmonic mean of $$\n\$$'s divisors is an integer, for example $$\6\$$. They are A001599 in the OEIS.

The first few Ore numbers are

1, 6, 28, 140, 270, 496, 672, 1638, 2970, 6200, 8128, 8190, ...


Point of interest: this sequence contains all the perfect numbers (see Wikipedia for a proof).

This is a standard challenge. You may choose which of the following three options to do:

• Take a positive integer $$\n\$$ and output the first $$\n\$$ Ore numbers.
• Take a positive integer $$\n\$$ and output the $$\n\$$th Ore number.
• You may use 0-indexing (so non-negative input) or 1-indexing, your choice
• Take no input, and output the never ending list of Ore numbers.

This is , so the shortest code in bytes wins.

• Brownie points for beating/matching my 9 byte Jelly answer Nov 23 '21 at 0:13
• Can our answers fail due to integer overflow errors? (I assume not, but just checking)
– user
Nov 26 '21 at 20:25
• @user I don't really care if they fail because the numbers involved exceeded a practical size, so long as it would work in theory, and that the failure isn't because of any floating point errors Nov 26 '21 at 20:58

# Factor + lists.lazy math.primes.factors math.unicode, 69 65 bytes

[ 1 lfrom [ divisors [ length 1 ] keep n/v Σ mod 0 = ] lfilter ]


Try it online!

It's a quotation that returns an infinite lazy list of the harmonic divisor numbers.

## Explanation

• 1 lfrom an infinite lazy list of natural numbers
• [ ... ] lfilter select numbers for which the quotation returns true
• divisors get the divisors of a number (e.g. 6 divisors -> { 1 2 3 6 })
• [ length 1 ] keep (e.g. { 1 2 3 6 } [ length 1 ] keep -> 4 1 { 1 2 3 6 })
• n/v divide number by vector (e.g. 1 { 1 2 3 6 } n/v -> { 1 1/2 1/3 1/6 })
• Σ take the sum
• mod 0 = is it a divisor?

# Raku, 46 bytes

grep {{@_%%sum 1 X/@_}(grep $_%%*,1..$_)},^∞


Try it online!

This is a lazy infinite sequence of the harmonic divisor numbers.

# R, 55 52 bytes

-3 bytes thanks to @Dominic van Essen.

while(F<-F+1)(1/mean(1/(y=1:F)[!F%%y]))%%1||print(F)


Try it online!

Prints Ore numbers infinitely.

• I was slower than you, but got 54 bytes; the approach is pretty similar, though... Nov 23 '21 at 9:02
• @Dominic, nice, combination of these approaches can give us 52 Nov 23 '21 at 9:04
• Hmm... I'm not sure that the step of taking the mean (or summing) reciprocals (mean(1/(y=1:F)[!F%%y]), or sum(1/k[d]) in the first version) will always satisfy "your answer cannot fail due to floating point errors". Nov 23 '21 at 10:47
• I think the FP error issue can be kind of avoided by summing the product-of-divisors divided by each divisor (so the division always yields an integer) in 70 bytes, but it's not really much of an improvement, because it goes out of R's integer range before encountering any 'Ore numbers' that would cause a floating point error... Nov 23 '21 at 10:48
• @Dominic, that's a tough one. I assumed that since (within reasonable limits) we don't encounter floating point errors, then current solution is fine. It's also not clear to me from this meta discussion, as this solution doesn't currently fail due to floats, but may - over some unreasonable bound. Nov 23 '21 at 11:47

# Jelly, 9 bytes

ÆDpWSḍ/µ#


Try It Online!

This is horribly scuffed because I couldn't figure out how to get it working with precision. I had the same idea as ovs it turns out, but ÆDÆmḍµ# fails due to precision issues.

I honestly hate how this is written.

ÆDpWSḍ/µ#    Main Link
µ#    nfind; return first n values satisfying:
ÆD           divisors of n
p          cartesian product with
W         [n] (returns [[a, n], [b, n], ...])
S        sum (returns [divisor sum, divisor count * n])
ḍ/      reduce by divisibility check


# Pari/GP, 42 bytes

for(n=1,oo,numdiv(n)*n%sigma(n)||print(n))


Try it online!

# Wolfram Language (Mathematica), 42 40 bytes

-2 bytes thanks to att!

Do[Mean@Divisors@n∣n&&Print@n,{n,∞}]


Try it online!

• 40 bytes
– att
Nov 24 '21 at 22:49
• How do you measure the bytes in Mathematica?
– EGME
Nov 25 '21 at 16:04
• @EGME Mathematica doesn't have any special encoding, so we just use UTF-8, where both ∣ and ∞ are encoded in 3 bytes.
– ovs
Nov 25 '21 at 16:07
• So how are you measuring the bytes then, for the whole thing? Do you just take the character string and see how much space it needs?
– EGME
Nov 25 '21 at 16:08
• Thanks, I think I got it … let’s see if I can better this :)
– EGME
Nov 25 '21 at 16:20

# Charcoal, 37 bytes

Ｎθ≔⁰ηＷθ«≦⊕η≔Φ⊕η∧κ¬﹪ηκζ¿¬﹪×ηＬζΣζ≦⊖θ»Ｉη


Try it online! Link is to verbose version of code. Outputs the 1-indexed nᵗʰ Ore number. Explanation:

Ｎθ


Input n.

≔⁰η


Start looking for Ore numbers greater than zero.

Ｗθ«


Repeat until the nᵗʰ number has been found.

≦⊕η


Try the next integer.

≔Φ⊕η∧κ¬﹪ηκζ


Get its factors.

¿¬﹪×ηＬζΣζ


If the harmonic mean is an integer, then...

≦⊖θ


Decrement the count of remaining Ore numbers to find.

»Ｉη


Print the found Ore number.

# Ruby, 71 56 bytes

1.step{|n|k=0;(1..n).count{|x|n%x<1&&k+=1r/x}%k>0||p(n)}


Try it online!

• Saved 15 thanks to @G B lots of golfs

Outputs the sequence indefinitely.

• Shorter
– G B
Nov 23 '21 at 12:03
• Even shorter
– G B
Nov 23 '21 at 12:06

# Jelly, 9 bytes

1Æd×ọÆsƲ#


Try it online!

More boring than the other answer:

             Implicit input: an integer z.
1      Ʋ#    Count up from 1, finding z numbers for which...
Æd×           divisor_count(n) × n
ọ          is divisible by
Æs        divisor_sum(n).

• Very nice, I had the same but with ×Æd instead! Nov 23 '21 at 19:50

# Core Maude, 248 bytes

mod H is pr LIST{Rat}. ops o h : Rat ~> Rat . var A B C D : Rat . eq o(A)= o(2
A). eq o(s A 0)= A . eq o(A s B)= o(s A(B + ceiling(frac(h(A A 0 0))))). eq h(A
s B C D)= h(A B(0 ^(A rem s B)/ s B + C)(0 ^(A rem s B)+ D)). eq h(A 0 C D)=
D / C . endm


The result is obtained by reducing the o function with the zero-indexed input $$\n\$$.

### Example Session

Maude> red o(0) .  --- 1
result NzNat: 1
Maude> red o(1) .  --- 6
result NzNat: 6
Maude> red o(2) .  --- 28
result NzNat: 28
Maude> red o(3) .  --- 140
result NzNat: 140
Maude> red o(4) .  --- 270
result NzNat: 270
Maude> red o(5) .  --- 496
result NzNat: 496
Maude> red o(6) .  --- 672
result NzNat: 672
Maude> red o(7) .  --- 1638
result NzNat: 1638
Maude> red o(8) .  --- 2970
result NzNat: 2970
Maude> red o(9) .  --- 6200
result NzNat: 6200
Maude> red o(10) .  --- 8128
result NzNat: 8128
Maude> red o(11) .  --- 8190
result NzNat: 8190


### Ungolfed

mod H is
pr LIST{Rat} .

ops o h : Rat ~> Rat .

var A B C D : Rat .

eq o(A) = o(2 A) .
eq o(s A 0) = A .
eq o(A s B) = o(s A (B + ceiling(frac(h(A A 0 0))))) .

eq h(A s B C D) = h(A B (0 ^ (A rem s B) / s B + C) (0 ^ (A rem s B) + D)) .
eq h(A 0 C D) = D / C .
endm


Maude has built-in support for rational arithmetic, so we just compute the harmonic mean of the divisors with h. Then, ceiling(frac(h(...))) will be 0 if h(...) is a natural number or 1 otherwise. Also, note that in Maude 0 ^ 0 == 1 and 0 ^ X = 0 for X =/= 1.

# Stax, 11 bytes

¡♪♫ö╪ü♣↕¥Vv


Run and debug it

Runs an infinite loop with no input.

# Zephyr, 151 bytes

set n to 1
while 1=1
set s to 0
set c to 0
for d from 1to n
if(n mod d)=0
set s to(/d)+s
inc c
end if
next
if((c/s)mod 1)=0
print n
end if
inc n
repeat


Try it online! Uses the output-infinitely strategy; you'll need to kill the program before 60 seconds in order to see any output.

### Ungolfed

# Start from 1
set num to 1
# Loop forever
while true
# Calculate the sum of the reciprocals of the divisors
# and also the total number of divisors
set reciprocalSum to 0
set divisorCount to 0
for divisor from 1 to num
if (num mod divisor) = 0
set reciprocalSum to reciprocalSum + (/ divisor)
inc divisorCount
end if
next
# Print the number if the divisor count divided by the
# divisor-reciprocal sum is an integer
if ((divisorCount / reciprocalSum) mod 1) = 0
print num
end if
# Go to the next number
inc num
repeat


# MathGolf, 13 bytes

î∙─‼Σ£î*\÷╛p∟


Outputs indefinitely.

Try it online. (You do have to manually cancel it during runtime to see output apparently, before it times out after 60 seconds..)

Explanation:

            ∟  # Do-while true without popping:
î              #  Push the 1-based loop-index
∙             #  Triplicate it
─            #  Pop the top, and get a list of its divisors
‼           #  Apply the following two commands separately:
Σ          #   Sum the divisors-list
£         #   Get the length of the divisors-list
î*       #  Multiply the length by the 1-based loop-index
\      #  Swap the top two values on the stack
÷     #  Check that the length*î is divisible by the sum
╛    #  If this is truthy:
p   #   Pop the remaining copy of the index, and print it


# Python 3, 79 bytes

n=0
while 1:n+=1;a=[i for i in range(1,n+1)if n%i<1];n*len(a)%sum(a)or print(n)


Try it online!

Outputs indefinitely.

# JavaScript (V8), 63 bytes

Prints the sequence forever.

{for(n=0;;s%t||print(n))for(k=++n,t=s=0;k;)n%k--||(s+=n,t-=~k)}


Try it online!

# Husk, 9 bytes

fo§¦ṁ\LḊN


Try it online! (header outputs the first few elements to avoid timing-out)

         N  # from the infinite list of integers
fo          # output those for which
ṁ\     # the sum of the reciprocals of their divisors
§¦        # exactly divides
LḊ   # the length (number) of their divisors


# Scala, 111 bytes

Stream.iterate(1:BigInt)(_+1)filter{n=>val d=n to(1,-1)filter(n%_<1)
val p=d.product
p*d.size%d.map(p./).sum<1}


Try it online!

Returns an infinite Stream.

## Scala, 92 bytes

Stream from 1 filter{n=>val d=1 to n filter(n%_<1)
val p=d.product
p*d.size%(0/:d)(_+p/_)<1}


Try it online!

This one uses normal Ints, evading some of the boilerplate above, but it only generates the first three elements correctly due to integer overflows.

# Python 3, 94 bytes

v=0
while 1:p=q=1;v+=1;-~len([(p:=p*d+q,q:=q*d)for d in range(2,v+1)if v%d<1])*q%p or print(v)


Try it online!

Outputs indefinitely. This is otherwise like (my edit to) @Jitse's answer, but computes the sum of the reciprocals p/q exactly as a pair of (big)ints (p,q).

• For what it's worth, your edit was rejected, as Jitse's answer works at the moment, and your edit would have made it reliant on floating points, making it prone to failing due to floating point errors. This answer is perfectly fine though Dec 4 '21 at 21:51

# Vyxal 2.6.1, 5 bytes

≬KṁḊȯ


Try it Online!

This doesn't work in 2.4 because 2.6 uses sympy rationals to store non-integers. Essentially a port of hyper's hypothetical Jelly answer.

## Explained

≬KṁḊȯ
≬     # The next three elements as a function, taking single argument n:
K    #    divisors of n
ṁ   #    the average of that
Ḋ  #    does that divide n?
ȯ # First input numbers that satisfy the above function.

• Why not check to see if the number of divisors is divisible by the sum of the reciprocals, rather than checking if the division is an integer? Nov 23 '21 at 1:33
• Because it doesn't work for cases like 28 @cairdcoinheringaahing Nov 23 '21 at 1:43
• @cairdcoinheringaahing also, floating point inaccuracies Nov 23 '21 at 1:46
• Having said that, that isn't an issue in the 2.6 pre-releases lol Nov 23 '21 at 1:49
• @Radek you need to download the v2.6.0rc2 build of vyxal and use the REPL Dec 5 '21 at 4:29