16
\$\begingroup\$

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.

Note that your answer cannot fail due to floating point errors.

This is , so the shortest code in bytes wins.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Brownie points for beating/matching my 9 byte Jelly answer \$\endgroup\$ Nov 23, 2021 at 0:13
  • \$\begingroup\$ Can our answers fail due to integer overflow errors? (I assume not, but just checking) \$\endgroup\$
    – user
    Nov 26, 2021 at 20:25
  • \$\begingroup\$ @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 \$\endgroup\$ Nov 26, 2021 at 20:58

21 Answers 21

4
\$\begingroup\$

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?
\$\endgroup\$
4
\$\begingroup\$

Raku, 46 bytes

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

Try it online!

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

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ I was slower than you, but got 54 bytes; the approach is pretty similar, though... \$\endgroup\$ Nov 23, 2021 at 9:02
  • \$\begingroup\$ @Dominic, nice, combination of these approaches can give us 52 \$\endgroup\$
    – pajonk
    Nov 23, 2021 at 9:04
  • \$\begingroup\$ 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". \$\endgroup\$ Nov 23, 2021 at 10:47
  • \$\begingroup\$ 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... \$\endgroup\$ Nov 23, 2021 at 10:48
  • \$\begingroup\$ @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. \$\endgroup\$
    – pajonk
    Nov 23, 2021 at 11:47
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

Pari/GP, 42 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Wolfram Language (Mathematica), 42 40 bytes

-2 bytes thanks to att!

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

Try it online!

\$\endgroup\$
15
  • 2
    \$\begingroup\$ 40 bytes \$\endgroup\$
    – att
    Nov 24, 2021 at 22:49
  • \$\begingroup\$ How do you measure the bytes in Mathematica? \$\endgroup\$
    – EGME
    Nov 25, 2021 at 16:04
  • \$\begingroup\$ @EGME Mathematica doesn't have any special encoding, so we just use UTF-8, where both and are encoded in 3 bytes. \$\endgroup\$
    – ovs
    Nov 25, 2021 at 16:07
  • \$\begingroup\$ 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? \$\endgroup\$
    – EGME
    Nov 25, 2021 at 16:08
  • 1
    \$\begingroup\$ Thanks, I think I got it … let’s see if I can better this :) \$\endgroup\$
    – EGME
    Nov 25, 2021 at 16:20
2
\$\begingroup\$

Charcoal, 37 bytes

Nθ≔⁰ηWθ«≦⊕η≔Φ⊕η∧κ¬﹪ηκζ¿¬﹪×ηLζΣζ≦⊖θ»Iη

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

Nθ

Input n.

≔⁰η

Start looking for Ore numbers greater than zero.

Wθ«

Repeat until the nᵗʰ number has been found.

≦⊕η

Try the next integer.

≔Φ⊕η∧κ¬﹪ηκζ

Get its factors.

¿¬﹪×ηLζΣζ

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

≦⊖θ

Decrement the count of remaining Ore numbers to find.

»Iη

Print the found Ore number.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Shorter \$\endgroup\$
    – G B
    Nov 23, 2021 at 12:03
  • \$\begingroup\$ Even shorter \$\endgroup\$
    – G B
    Nov 23, 2021 at 12:06
2
\$\begingroup\$

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).
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Very nice, I had the same but with ׯd instead! \$\endgroup\$ Nov 23, 2021 at 19:50
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Stax, 11 bytes

¡♪♫ö╪ü♣↕¥Vv

Run and debug it

Runs an infinite loop with no input.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Pyt, 22 bytes

1`ĐðĐĐΠ⇹/Ʃ⇹Π|?ŕĐƥ:ŕ;⁺ł

Try it online!

Prints Ore numbers forever.

1                              Push 1
 `                   ł         Do... while top of stack is truthy
  Đ                            Đuplicate
   ð                           Get list of ðivisors
    ĐĐ                         Đuplicate twice
      Π                        Get product
       ⇹                       Swap top two elements on stack
        /                      Divide
         Ʃ                     Ʃum
          ⇹                    Swap top two elements
           Π|?                 Does the sum divide the product cleanly?
              ŕĐƥ              If so, ŕemove the boolean, Đuplicate, and ƥrint
                 :ŕ            Otherwise, ŕemove the boolean
                   ;⁺          Either way, increment
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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).

\$\endgroup\$
1
  • \$\begingroup\$ 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 \$\endgroup\$ Dec 4, 2021 at 21:51
1
\$\begingroup\$

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.
\$\endgroup\$
6
  • \$\begingroup\$ 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? \$\endgroup\$ Nov 23, 2021 at 1:33
  • \$\begingroup\$ Because it doesn't work for cases like 28 @cairdcoinheringaahing \$\endgroup\$
    – lyxal
    Nov 23, 2021 at 1:43
  • \$\begingroup\$ @cairdcoinheringaahing also, floating point inaccuracies \$\endgroup\$
    – lyxal
    Nov 23, 2021 at 1:46
  • \$\begingroup\$ Having said that, that isn't an issue in the 2.6 pre-releases lol \$\endgroup\$
    – lyxal
    Nov 23, 2021 at 1:49
  • 1
    \$\begingroup\$ @Radek you need to download the v2.6.0rc2 build of vyxal and use the REPL \$\endgroup\$
    – lyxal
    Dec 5, 2021 at 4:29
1
\$\begingroup\$

Nibbles, 8.5 bytes

|,~~%*,;|,$~%@$@+

Attempt This Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.