17
\$\begingroup\$

A \$k\$-hyperperfect number is a natural number \$n \ge 1\$ such that

$$n = 1 + k(\sigma(n) − n − 1)$$

where \$\sigma(n)\$ is the sum of the divisors of \$n\$. Note that \$\sigma(n) - n\$ is the proper divisor sum of \$n\$. The sequence of \$k\$-hyperperfect numbers begins

$$6, 21, 28, 301, 325, 496, 697, \dots$$

This is A034897 on the OEIS.

For example:

$$\begin{align} \sigma(21) & = 1 + 3 + 7 + 21 = 32 \\ 21 & = 1 + 2(32 - 21 - 1) \\ & = 1 + 2 \times 10 \\ & = 21 \end{align}$$

Therefore, \$21\$ is a \$2\$-hyperperfect number.


You are to take a \$k\$-hyperperfect number \$n\$ as input and output the value of \$k\$. You can assume you will never have to handle numbers greater than your language's limit at any point (i.e. \$k\sigma(n)\$ will always be within the bounds of your language), and you may input and output in any convenient format.

The input is guaranteed to be a \$k\$-hyperperfect number, you don't have to handle inputs such as \$2, 87, 104\$ etc. that aren't \$k\$-hyperperfect.

This is , so the shortest code in bytes wins.

Test cases

These are the outputs for all the listed values in the OEIS for this sequence, and are the exhaustive list of inputs for \$n < 1055834\$

      n       k
      6       1
     21       2
     28       1
    301       6
    325       3
    496       1
    697      12
   1333      18
   1909      18
   2041      12
   2133       2
   3901      30
   8128       1
  10693      11
  16513       6
  19521       2
  24601      60
  26977      48
  51301      19
  96361     132
 130153     132
 159841      10
 163201     192
 176661       2
 214273      31
 250321     168
 275833     108
 296341      66
 306181      35
 389593     252
 486877      78
 495529     132
 542413     342
 808861     366
1005421     390
1005649     168
1055833     348
\$\endgroup\$
3
  • \$\begingroup\$ Related. Related. Brownie points for beating my 7 byte Jelly answer \$\endgroup\$ Apr 16, 2021 at 21:21
  • 4
    \$\begingroup\$ They missed a chance to call them "hyperfect" numbers \$\endgroup\$
    – Jo King
    Apr 16, 2021 at 22:40
  • 1
    \$\begingroup\$ @JoKing Your comment made me realize that it was not spelled "hyperfect". :-p \$\endgroup\$
    – Arnauld
    Apr 16, 2021 at 23:15

16 Answers 16

13
\$\begingroup\$

Jelly, 5 bytes

:Æṣ’$
:       integer divide
Æṣ’$    the decremented divisor sum

This uses \$\lfloor\frac{n}{\sigma(n)-n-1}\rfloor\$ instead of \$\frac{n-1}{\sigma(n)-n-1}\$, but it still works because \$\frac{1}{\sigma(n)-n-1}\$ can never be greater than or equal to 1.

Try it online!

\$\endgroup\$
8
\$\begingroup\$

J, 24 23 20 bytes

<.@%1<:@#.[:I.0=i.|]

Try it online!

-3 thanks to rak's rounding down trick

Let n be the input.

  • <.@% round down n divided by
  • 1<:@#. 1 minus the sum of
  • [:I. the indexes where
  • 0= 0 is equal to the remainder when
  • i. the list 0..n-1
  • | is divided elementwise into
  • ] n.
\$\endgroup\$
7
\$\begingroup\$

JavaScript (ES6), 36 bytes

n=>~-n/(g=k=>~k--&&!(n%k)*k+g(k))(n)

Try it online!

How?

\$\sigma(n) − n − 1\$ is the sum of the divisors of \$n\$ in \$[2..n-1]\$. This is also the sum of the divisors of \$n\$ in \$[-1..n-1]\$ because \$1\$ and \$-1\$ cancel each other out. The helper function \$g\$ computes the latter, so that we can use the slightly more golf-friendly halt condition ~k--.

Note: From a mathematical perspective, we really should remove \$0\$ from the list of possible divisors. But it is quietly ignored in this code as we get !NaN*0, which is \$0\$.

\$\endgroup\$
5
\$\begingroup\$

Wolfram Language (Mathematica), 25 bytes

--+#/(Tr@Divisors@#-#-1)&

Try it online!

-1 byte from @att

\$\endgroup\$
1
  • \$\begingroup\$ 25 bytes \$\endgroup\$
    – att
    Apr 16, 2021 at 21:39
5
\$\begingroup\$

Haskell, 38 bytes

k n=div n$sum[d|d<-[2..n-1],mod n d<1]

Try it online!

\$\endgroup\$
0
5
\$\begingroup\$

Japt, 12 6 bytes

zUâ ¤x

Try it

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I believe this is the first time I've seen the pairing of values returned by N.â() that was introduced in v1.4.6 actually be useful. Nice one! \$\endgroup\$
    – Shaggy
    Apr 17, 2021 at 21:29
  • \$\begingroup\$ Yeah! Thanks @Shaggy ! I was wondering why not ordered ? when I realized how useful in that case ! \$\endgroup\$
    – AZTECCO
    Apr 17, 2021 at 21:48
5
\$\begingroup\$

V (vim), 81 79 70 bytes

"aD@ai0
<esc>V{g<c-a>{dd}dkqqC<c-r>=!(<c-r>a%<c-r>")*<c-r>"
<esc>k@qq@q:%s/\n/+
$x0C<c-r>=<c-r>a/(<c-r>")

Try it online!

Uses the observation in rak1507's Jelly answer.

-2 bytes from kops.

-9 more bytes from kops, removing the entire third line!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I haven't tried to figure out what this is doing but, as you guessed, the dd in the first line can be removed. It's a no-op since you can't remove a line from an empty file. \$\endgroup\$
    – kops
    Apr 17, 2021 at 6:30
  • \$\begingroup\$ A bit more golfing -- I think the third line of commands i mostly unnecessary by just throwing *<c-r>" into the first formula. Nice solution; I didn't know about visual mode g<c-a>! \$\endgroup\$
    – kops
    Apr 17, 2021 at 6:45
  • \$\begingroup\$ I found Vg<c-a> while snooping around in the LoTM thread. Thanks for the help! \$\endgroup\$
    – Razetime
    Apr 17, 2021 at 6:54
4
\$\begingroup\$

05AB1E, 5 bytes

Ties rak1507's Jelly answer for #1.

ѨO<÷

Try it online!

ѨO<÷  # full program
       # implicit input...
    ÷  # divided by...
  O    # sum of...
Ñ      # divisors of...
       # implicit input...
 ¨     # excluding the last...
    ÷  # rounded down
       # implicit output
\$\endgroup\$
4
\$\begingroup\$

R, 35 bytes

function(x,y=x-1)y/sum(2:y*!x%%2:y)

Try it online!

Inspired by @Dominic's answer to linked challenge.

Uses the fact that \$\sigma(n)-n-1\$ for n>2 is just sum of divisors that lie between 2 and n-1. Then, we simply divide n-1 by the obtained value to get k (as n is guaranteed to be hyperperfect).

R, 34 bytes

function(x)x%/%sum(2:x*!x%%2:x,-x)

Try it online!

Using @rak's formula.

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

C (gcc), 45 44 bytes

i,t;k(x){for(t=i=1;++t<x;)i-=x%t?0:t;x/=-i;}

Try it online!

  • uses Jelly's formula

  • saved 1 by sum negatively and starting from 1

Explanation

   i and t used to get divisors sum
   for(t=i=1;++t<x;) - we start our loop with t=1 and end before x to exclude them from dividers 
   i-=x%t?0:t;       - we iterate all values and add to i negatively if modulo t is 0
   x/=-i;           - finally we return trough eax trick x divided by -sum which started from 1 so we have the +1 term added to k(sum)
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Can you explain a little bit about what this code does? I'm very intrigued by it. Especially, I don't see any type declaration or return statement anywhere. In the Try it online you posted k() is also called with 2 arguments even though it was only defined with 1 argument. \$\endgroup\$
    – Mia
    Apr 17, 2021 at 21:08
  • \$\begingroup\$ @Jerie Wang forget about the k calls with 2 arguments.. Just a type error.. It works only because it ignores the second argument. For type declarations C defaults global variables and function parameters to int (it just live a warning) . The missing return is a famous trick in C: it uses eax register, note the final assignment to x , this is equivalent to the return statement some explanation here . I'll add explanation soon in the answer \$\endgroup\$
    – AZTECCO
    Apr 17, 2021 at 22:04
3
\$\begingroup\$

Retina 0.8.2, 54 bytes

.+
$*
1(?=(1(?<=(?=(?(\3+$)(\2?\3)))(1+)))+1)(\2)+
$#4

Try it online! Link includes faster test cases. Explanation:

.+
$*

Convert the input to unary.

1(?=(1(?<=(?=(?(\3+$)(\2?\3)))(1+)))+1)(\2)+
$#4

Based on @Deadcode's answer to Am I not good enough for you? this calculates the sum of divisors from 2 to n-1, but golfed down (removed a ^ and two $s) by assuming that the input is a hyperperfect number. After the sum of divisors is calculated the result is then divided into n-1.

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

Python 3, 46 45 bytes

1 byte off thanks to @dingledooper! Also, this uses the floor trick from @rak1507's answer.

lambda n:n//sum(k*(n%k<1)for k in range(2,n))

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ 45 bytes \$\endgroup\$ Apr 16, 2021 at 22:58
  • \$\begingroup\$ @dingledooper Thank you! I had lambda n:n//sum(k for k in range(2,n)if n%k<1) but it was still 46 bytes \$\endgroup\$
    – Luis Mendo
    Apr 16, 2021 at 22:59
  • \$\begingroup\$ @LuisMendo you haven't updated the code \$\endgroup\$
    – rak1507
    Apr 16, 2021 at 23:02
  • 1
    \$\begingroup\$ @rak1507 Updated now, thanks! \$\endgroup\$
    – Luis Mendo
    Apr 16, 2021 at 23:07
  • 1
    \$\begingroup\$ That would be one byte shorter in Python 2 (using / instead of //). \$\endgroup\$
    – Arnauld
    Apr 16, 2021 at 23:38
2
\$\begingroup\$

K (oK), 18 bytes

{_x%+/1_&~(!x)!'x}

Try it online!

A port of @Jonah's J answer - don't forget to upvote him!

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

Vyxal, 4 bytes

∆K‹ḭ

Try it Online!

Porting the jelly answer ftw

Explained

∆K‹ḭ
∆K   # sum of proper divisors of input
  ‹  # ↑ - 1
   ḭ # input // ↑ (integer division)
\$\endgroup\$
3
  • \$\begingroup\$ why??? why is ∆K a thing?? \$\endgroup\$
    – Makonede
    Apr 17, 2021 at 2:18
  • \$\begingroup\$ @Makonede because jelly has it too \$\endgroup\$
    – lyxal
    Apr 17, 2021 at 2:24
  • \$\begingroup\$ Oh that took me a second... I thought that it took \$3\$ bytes (like in 05AB1E) because I forgot that $ was a quick to combine links into a monad \$\endgroup\$
    – Makonede
    Apr 17, 2021 at 2:34
1
\$\begingroup\$

Excel, 52 bytes

=LET(n,A2-1,q,SEQUENCE(n),n/SUM((MOD(A2,q)=0)*q,-1))

Works except for the last test case. SEQUENCE is limited to 2^20. The following works up to 2^40 and is 79 bytes.

=LET(q,SEQUENCE(A2^0.5),a,(MOD(A2,q)=0)*q,(A2-1)/(SUM(a,IFERROR(A2/a,0))-A2-1))
\$\endgroup\$
1
\$\begingroup\$

Pari/GP, 23 bytes

n\sum(i=2,n-1,!(n%i)*i)
\$\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.