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
  • \$\begingroup\$ I'm not entirely sure, but I think you can remove the ² and use implicit inputs. It will in that case incorrectly multiply the first input \$m\$ with \1ドル\$ in the first iteration, but I think (based on the test cases, so I'm not sure) \1ドル\$ will never be in the output anyway, so it shouldn't matter. But it's likely a counter-example could be found where it might incorrectly result in truthy for \1ドル\$ if you have \$m\$ instead of \$k\$ in the first iteration? Not sure how to prove or disprove my hunch. \$\endgroup\$ Commented Mar 12, 2021 at 10:13
  • 1
    \$\begingroup\$ @KevinCruijssen this fails for \$m=1\$ and any \$k>1\$ as \$\sigma(1)=1\$. I remember spending some time on trying to use implicit input here without success. \$\endgroup\$ Commented Mar 12, 2021 at 10:58
  • 1
    \$\begingroup\$ Hmm, and if you swap the two, and use the implicit second input after dividing by the current number at the end: ∞ʒ¹FÑO}y/Q? (Not sure how floating point inaccuracies might affect the results, though.) \$\endgroup\$ Commented Mar 12, 2021 at 11:09
  • 1
    \$\begingroup\$ @KevinCruijssen thanks a lot, this does work :). And as long as k is reasonably small, no floating point issues should occur. \$\endgroup\$ Commented Mar 12, 2021 at 11:25

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