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 sequence 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 code-golf, so the shortest code in bytes wins.
-
1\$\begingroup\$ Brownie points for beating/matching my 9 byte Jelly answer \$\endgroup\$caird coinheringaahing– caird coinheringaahing ♦2021年11月23日 00:13:45 +00:00Commented Nov 23, 2021 at 0:13
-
\$\begingroup\$ Can our answers fail due to integer overflow errors? (I assume not, but just checking) \$\endgroup\$user– user2021年11月26日 20:25:24 +00:00Commented 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\$caird coinheringaahing– caird coinheringaahing ♦2021年11月26日 20:58:06 +00:00Commented Nov 26, 2021 at 20:58
25 Answers 25
Factor + lists.lazy math.primes.factors math.unicode, (削除) 69 (削除ここまで) 65 bytes
[ 1 lfrom [ divisors [ length 1 ] keep n/v Σ mod 0 = ] lfilter ]
It's a quotation that returns an infinite lazy list of the harmonic divisor numbers.
Explanation
1 lfroman infinite lazy list of natural numbers[ ... ] lfilterselect numbers for which the quotation returnstruedivisorsget 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/vdivide number by vector (e.g.1 { 1 2 3 6 } n/v->{ 1 1/2 1/3 1/6 })Σtake the summod 0 =is it a divisor?
Raku, 46 bytes
grep {{@_%%sum 1 X/@_}(grep $_%%*,1..$_)},^∞
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)
Prints Ore numbers infinitely.
-
\$\begingroup\$ I was slower than you, but got 54 bytes; the approach is pretty similar, though... \$\endgroup\$Dominic van Essen– Dominic van Essen2021年11月23日 09:02:19 +00:00Commented Nov 23, 2021 at 9:02
-
-
\$\begingroup\$ Hmm... I'm not sure that the step of taking the mean (or summing) reciprocals (
mean(1/(y=1:F)[!F%%y]), orsum(1/k[d])in the first version) will always satisfy "your answer cannot fail due to floating point errors". \$\endgroup\$Dominic van Essen– Dominic van Essen2021年11月23日 10:47:35 +00:00Commented 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\$Dominic van Essen– Dominic van Essen2021年11月23日 10:48:52 +00:00Commented 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– pajonk2021年11月23日 11:47:07 +00:00Commented Nov 23, 2021 at 11:47
Jelly, 9 bytes
ÆDpWSḍ/μ#
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
Wolfram Language (Mathematica), (削除) 42 (削除ここまで) 40 bytes
-2 bytes thanks to att!
Do[Mean@Divisors@n∣n&&Print@n,{n,∞}]
-
2
-
\$\begingroup\$ How do you measure the bytes in Mathematica? \$\endgroup\$EGME– EGME2021年11月25日 16:04:39 +00:00Commented 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– ovs2021年11月25日 16:07:01 +00:00Commented 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– EGME2021年11月25日 16:08:26 +00:00Commented Nov 25, 2021 at 16:08
-
1\$\begingroup\$ Thanks, I think I got it ... let’s see if I can better this :) \$\endgroup\$EGME– EGME2021年11月25日 16:20:33 +00:00Commented Nov 25, 2021 at 16:20
Pyt, 22 bytes
1`ĐðĐĐΠ⇹/Ʃ⇹Π|?ŕĐƥ:ŕ;+ł
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
Charcoal, 37 bytes
×ばつηLζΣζ≦⊖θ»Iη
Try it online! Link is to verbose version of code. Outputs the 1-indexed nth Ore number. Explanation:
Nθ
Input n.
≔0η
Start looking for Ore numbers greater than zero.
Wθ«
Repeat until the nth 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.
Ruby, (削除) 71 (削除ここまで) 56 bytes
1.step{|n|k=0;(1..n).count{|x|n%x<1&&k+=1r/x}%k>0||p(n)}
- Saved 15 thanks to @G B lots of golfs
Outputs the sequence indefinitely.
-
-
\$\begingroup\$ Even shorter \$\endgroup\$G B– G B2021年11月23日 12:06:43 +00:00Commented Nov 23, 2021 at 12:06
Jelly, 9 bytes
1Æd×ばつọÆsƲ#
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).
-
1\$\begingroup\$ Very nice, I had the same but with
ׯdinstead! \$\endgroup\$2021年11月23日 19:50:39 +00:00Commented Nov 23, 2021 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.
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
TI-BASIC (TI-84 Plus CE Python), (削除) 74 67 (削除ここまで) 65 bytes
While 1
Ans+1
If not(fPart(round(AnsΣ(int(Ans/K)-int((Ans-1)/K),K,1,1+Ans)/Σ(Knot(fPart(Ans/K)),K,1,1+Ans),9
Disp Ans
End
make sure Ans is set to 0 before running.
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
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)}
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}
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}
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)
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).
-
\$\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\$2021年12月04日 21:51:24 +00:00Commented Dec 4, 2021 at 21:51
Vyxal 2.6.1, 5 bytes
≬KṁḊȯ
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.
-
\$\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\$2021年11月23日 01:33:22 +00:00Commented Nov 23, 2021 at 1:33
-
\$\begingroup\$ Because it doesn't work for cases like
28@cairdcoinheringaahing \$\endgroup\$2021年11月23日 01:43:26 +00:00Commented Nov 23, 2021 at 1:43 -
\$\begingroup\$ @cairdcoinheringaahing also, floating point inaccuracies \$\endgroup\$2021年11月23日 01:46:19 +00:00Commented Nov 23, 2021 at 1:46
-
\$\begingroup\$ Having said that, that isn't an issue in the 2.6 pre-releases lol \$\endgroup\$2021年11月23日 01:49:48 +00:00Commented 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\$2021年12月05日 04:29:38 +00:00Commented Dec 5, 2021 at 4:29
YASEPL, 81 bytes
=n!+=k$=f=o`1=g$n-/k(=l$n/k(-g!f+l=u$n/k%1+[2!o+k`2!k+}4,n!o+.9(!f/o*n%1+[3>n`3?3
this prints them infinitely
explanation
=n main increment
(after this is where the loop starts)
!+ add 1 to n
=k$ K variable for summation
=f F is total amount of divisors of N
=o O is sum of divisors of N
`1 start sum
( calculating number of divisors )
=g$n-/k(=l$n/k(-g l = floor(n/k) - floor(n-1/k)
!f+l add to sum
( calculating sum of divisors )
=u$n/k%1+[2 if (n/k) = 0...
!o+k add K to O
`2 end if
!k+ increment K
}4,n if K <= N, go back to `1
!o+.9( ceil O
!f/o*n%1+[3 if (f/o)*n = 0...
>n print n
`3 endif
?3 go back to the third char of the program (where !+ is)
this assumes that \$ n \$ is a harmonic divisor number if the following is true:
$$ 0=(\frac{\sum_{k=1}^{n}(\left\lfloor\frac{n}{k}\right\rfloor-\left\lfloor\frac{n-1}{k} \right\rfloor)}{\left\lceil\sum_{i=1}^{n}(0^{\frac{n}{i}\mod1}i)\right\rceil}\times n)\mod1 $$ I'm not the best with writing math equations so take it with a grain of salt.
this thing gets really slow.
APL(NARS), 80 chars
r←F w;x;m;k×ばつ⍳∼1=÷1∨(≢m)÷+/÷m←k/⍨0=x∣⍨k←⍳x
r,←x⋄w-←1⋄→2
// +/ 12 9 8 38 13=80
F 1-indexed, and it is slow it seems has at last O(n^5) if goes well. F use rationals.
÷1∨k finds the denominator of k rational.
Test:
F 5
1 6 28 140 270
F 10
1 6 28 140 270 496 672 1638 2970 6200
F 1
1
jBasher2, 592 bytes
create n with type number
while 1 == 1
add n by 1
set that to n
create k with type number
set 1 to k
create f with type number
create o with type number
while k <= n
create g with type number
subtract n by 1
divide that by k
parse that as int
set that to g
divide n by k
parse that as int
subtract that by g
add f by that
set that to f
divide n by k
modulo that by 1
if that == 0
add k by o
set that to o
endif
add 1 by k
set that to k
endwhile
divide 1 by 10
add o by that
parse that as int
set that to o
divide f by o
multiply that by n
modulo that by 1
if that == 0
output n
endif
endwhile
awesome translation of my YASEPL answer
Explore related questions
See similar questions with these tags.