Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Generalise perfect numbers

Let \$\sigma(n)\$ represent the divisor sum of \$n\$ and \$\sigma^m(n)\$ represent the repeated application of the divisor function \$m\$ times.

Perfect numbers are numbers whose divisor sum equals their double or \$\sigma(n) = 2n\$. For example, \$\sigma(6) = 12 = 2\times6\$

Superperfect numbers are numbers whose twice iterated divisor sum equals their double. For example, \$\sigma^2(16) = \sigma(\sigma(16)) = \sigma(31) = 32 = 2\times16\$

\$m\$-superperfect numbers are numbers such that \$\sigma^m(n) = 2n\$ for \$m \ge 1\$. For \$m \ge 3\$, there are no such numbers.

\$(m,k)\$-perfect numbers are numbers such that \$\sigma^m(n) = kn\$. For example, \$\sigma^3(12) = 120 = 12\times10\$, so \12ドル\$ is a \$(3,10)\$-perfect number.

You are to choose one of the following three tasks to do:

  • Take three positive integers \$n, m, k\$ and output the \$n\$th \$(m,k)\$-perfect number (0 or 1 indexed, your choice)
  • Take three positive integers \$n, m, k\$ and output the first \$n\$ \$(m,k)\$-perfect numbers
  • Take two positive integers \$m, k\$ and output all \$(m,k)\$-perfect numbers

You may assume that the inputs will never represent an impossible sequence (e.g. \$m = 5, k = 2\$) and that the sequences are all infinite in length. You may take input in any convenient method.

Note that methods that count up starting from either \$m\$ or \$k\$ are not valid, as they fail for \$(4,4)\$-perfect numbers, the smallest of which is \2ドル\$ (credit to Carl Schildkraut for finding this)

This is so the shortest code in bytes wins.


Test cases

This lists the first few outputs\${}^*\$ for example inputs of \$(m, k)\$

m, k -> out
3, 10 -> 12, 156, 32704, ...
2, 2 -> 2, 4, 16, 64, 4096, 65536, ...
1, 2 -> 6, 28, 496, 8128, ...
4, 48 -> 160, 455, 5920, ...
3, 28 -> 4480, ...
3, 16 -> 294, 6882, ...
1, 4 -> 30240, 32760, ...
4, 4 -> 2, ...

\${}^*\$: Aka, the outputs I could get from my generating program without timing out on TIO

Answer*

Draft saved
Draft discarded
Cancel
4
  • 2
    \$\begingroup\$ $x=1 instead $x=$n=1? \$\endgroup\$ Commented Mar 1, 2021 at 18:25
  • \$\begingroup\$ @mazzy Good spot! \$\endgroup\$ Commented Mar 1, 2021 at 18:31
  • \$\begingroup\$ $x=1 is redundant. and ,$x*!($a-$k*$x) instead ,$x*($a-eq$k*$x). Try it online!. thanks Zaelin, smart solution \$\endgroup\$ Commented Mar 1, 2021 at 19:04
  • \$\begingroup\$ Ahhhh, I had the x=1 in there because I was testing it in a console, so x needed reset every time; definitely wouldn't have thought to cut it; smart saves! Thanks again @mazzy! \$\endgroup\$ Commented Mar 1, 2021 at 19:11

AltStyle によって変換されたページ (->オリジナル) /