25
\$\begingroup\$

The objective

Given the non-negative integer \$n\$, output the value of the hyperfactorial \$H(n)\$. You don't have to worry about outputs exceeding your language's integer limit.

Background

The hyperfactorial is a variant of the factorial function. is defined as $$ H(n) = 1^{1} \cdot 2^{2} \cdot 3^{3} \cdot \: \cdots \: \cdot n^{n} $$

For example, \$H(4) = 1^{1} \cdot 2^{2} \cdot 3^{3} \cdot 4^{4} = 27648\$.

Test cases

n   H(n)
0   1
1   1
2   4
3   108
4   27648
5   86400000
6   4031078400000
7   3319766398771200000
8   55696437941726556979200000

Rules

\$\endgroup\$
2
  • \$\begingroup\$ I think one might be able to write a competitive 4 bit assembler (or even 8 bit assembler) answer which is a tiny LUT. \$\endgroup\$
    – abligh
    Commented Oct 5, 2021 at 2:40
  • \$\begingroup\$ oeis.org/A002109 \$\endgroup\$
    – bigyihsuan
    Commented Nov 14, 2022 at 19:32

77 Answers 77

1 2
3
1
\$\begingroup\$

Vyxal, 7 bytes

?ɾƛ:e;Π

Try it Online!

Will add an explanation soon.

\$\endgroup\$
1
  • \$\begingroup\$ This can be ɾ:eΠ for 4 bytes - input is implicitly taken if there's nothing on the stack and e vectorises (applies to each item in each list) by default \$\endgroup\$
    – lyxal
    Commented Dec 30, 2021 at 10:19
1
\$\begingroup\$

Excel, 33 bytes

=LET(x,SEQUENCE(A1),PRODUCT(x^x))

Link to Spreadsheet

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

Python + mpmath, 28 bytes

from mpmath import*
hyperfac

Attempt This Online!

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

Knight, 30 bytes

;=p=w 1;=qP;W<w q=p*p^=w+wTwOp

Try it online!

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

Knight (v2.0-alpha), 27 bytes

;;=i^=n+0PnW=n-nT=i*i^n nOi

Try it online!

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

J, 11 bytes

[:*/1^~@+i.

Attempt This Online!

[:*/1^~@+i.
[:          NB. enforce f (g x)
  */        NB. product reduce
         i. NB. range 0..x-1
    1   +   NB. add one
       @    NB. then
     ^~     NB. raise each item to itself
\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 15 bytes

+/*/'{x#x}'1+!:

Try it online!

Another quick answer.

Explanations:

+/*/'{x#x}'1+!:  Main function. Takes implicit input with :
             !   Range from [0..input-1]
           1+    + 1 to turn it into [1..input]
          '      For each number...
     {   }       Execute a function that...
        x        Takes the number as implicit variable x
      x#         And duplicate them x amount of times
    '            For each of the lists inside...
  */             Fold and multiply them
+/               Sum
\$\endgroup\$
1
\$\begingroup\$

><>, 42 bytes

1$01.11-1<
>:>:?v~:?^~l1=?n*a1.
1-^  >$:}$

Try it online

Explanation

Initializes product as 1 to handle the n=0 special case.
Loops over x from n down to 1 and puts x copies of x on the stack.
Ex: n=3 results in 3,3,3,2,2,1
Then takes the product of all numbers on the stack to get the result.

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

Arturo, 29 22 bytes

$=>[∏map..1&'x->x^x]

Try it

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

Lua, 38 bytes

a=1 for i=1,...do a=a*i^i end print(a)

Try it online!

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

Uiua, 7 bytes

/×ⁿ.+1⇡

Try it!

/×ⁿ.+1⇡
    +1⇡  # 1..n
   .     # duplicate
  ⁿ      # power
/×       # product
\$\endgroup\$
1
\$\begingroup\$

Rust, 40 bytes

|n|(1u32..=n).map(|n|n.pow(n)).product()

Attempt This Online!

Staightforward functional approach. Follows the rules for type inference. As the program takes and returns a u32, only calls up to H(5) are supported. This is because {integer}::pow takes a u32, and casting would cost five bytes.

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

Arn, 5 bytes

Uses the slightly older online version

O«▒¹Ù

Try it!

Explained

Unpacked: *{^}\~

         _    Implied variable, initialized to input
        ~    Range 1..
   *…\  fold with multiplication after mapping
  {   block with key of _, initialized to current number
 ^  raise _ to the power of _
} end block
\$\endgroup\$
0
\$\begingroup\$

Assembly (NASM, 32-bit, Linux), 85 bytes

H:mov ebx,eax
or ax,0
mov ax,1
je z
n:mov ecx,ebx
x:mul ebx
loop x
dec bx
jnz n
z:ret

Try it online!

The argument to the H function is passed in the eax register. The result is also in the eax register. The input has to be lower than 65,536.

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

Cognate, 36 bytes

(Let N;For Range 1 + 1 N(* ^ Twin)1)

Attempt This Online!

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

Pyt, 3 bytes

řṖΠ

Try it online!

ř            implicit input (n); push [1,2,3,...,n]
 Ṗ           raise each element to the power of itself
  Π          take the product of all of them; implicit print
\$\endgroup\$
0
\$\begingroup\$

SageMath, 35 bytes

Without using NATIVE python module or syntax

Golfed vesrion. Run it on SageMathCell!

f=lambda n:prod(i^i for i in[1..n])
\$\endgroup\$
1 2
3

Your Answer

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

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