28
\$\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

85 Answers 85

1 2
3
1
\$\begingroup\$

F# (.NET Core), 46 bytes

(Only works up to n = 5, however.)

let rec h=function 0->1|n->(pown n n)*(h(n-1))

Try it online!

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

PHP -F, 42 39 bytes

for($r=1;$argn-$n++;)$r*=$n**$n;echo$r;

Try it online!

No PHP yet, well this is The Answer! Unfortunately I had to resort to incrementing first, because of a weird operator precedence when using $n**$n-- ($n should decrement after the exponentiation if I RTM correctly, but actually it decrements first)

EDIT: nice improvement from Hydrazer, -3 bytes with a way to avoid using $n twice in the loop declaration

\$\endgroup\$
2
  • \$\begingroup\$ incrementing with $n is shorter 39 bytes \$\endgroup\$
    – scpchicken
    Commented Nov 1, 2021 at 14:39
  • \$\begingroup\$ @Hydrazer nice one! thanks \$\endgroup\$
    – Kaddath
    Commented Nov 2, 2021 at 9:51
1
\$\begingroup\$

Red, 40 bytes

func[n][a: 1 repeat i n[a: i ** i * a]a]

Try it online!

\$\endgroup\$
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\$

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
\$\begingroup\$

Lua, 38 bytes

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

Try it online!

\$\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\$
1
\$\begingroup\$

Japt, 6 5 bytes

ô*² ×

Try it here

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

Uiua, 12 bytes, 7 characters

/×ⁿ.+1⇡

Try it on Uiua Pad!

Explained

Uiua reads code from right to left, so this code will also be explained in that order.

+1⇡ // Make an array of numbers from 0 to n and increment each element 
.   // Create a copy of the array
ⁿ   // Raise each element to the power of itself
/×  // Reducing function: multiplicate each element by the next element on the array
\$\endgroup\$
1
\$\begingroup\$

Vyxal 3, 4 bytes

ɾ:*Π

Try it Online!

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

Setanta, 48 bytes

gniomh f(n){toradh(n&cmhcht@mata(n,n)*f(n-1))|1}

Try on try-setanta.ie

\$\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\$

awk

a hyperfactorial function in POSIX-compliant awk syntax that bypasses any and all usage of the exponentiation operator, without sacrificing efficiency :

jot 20 | gawk -Mbe '

function _____(__, _, ___, ____) {

    if ((_ = ___ = __ = int(__)) <= (____ += ____ = !!_))

        return (_ = __ < ____) ? _ : __ + __
 
    while (____ < --_)___ *= (__ *= _) * (__ *= --_)

    return _ < ____ ? ___ *  __ \
                    : ___ * (__ *= _) * __

} $++NF = _____($!_)'

1 1
2 4
3 108
4 27648
5 86400000
6 4031078400000
7 3319766398771200000
8 55696437941726556979200000
9 21577941222941856209168026828800000
10 215779412229418562091680268288000000000000000
11 61564384586635053951550731889313964883968000000000000000
12 548914237009501581804104224704637116078267727827959808000000000000000
13 166252458044258018207674078620690924617735088270974773221032328167424000000000000000
14 1847398448553592782012673311296877599223436283900539192451554723195762806303473270784000000000000000

UPDATE

adding a functional programming style recursive function in awk (but this version using the exponentiation operator) :

func __(_) { return _^_--*(1<_?__(_):1) }

awk's syntax looks really modern for a 50-year old language

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

C#, 77 bytes

n=>Enumerable.Range(0,n+1).Select(i=>Math.Pow(i,i)).Aggregate(1.0,(a,b)=>a*b)

Try it online!

Boring answer, but C# doesn't really have fancy ways of doing this. Even with the addition of the range operator (0..n+1), that produces a System.Range, which is not an IEnumerable and so can't be used as an iterator :(

1.0 in the call to Aggregate because it expects the start value and the return type of the predicate to be exactly equal, and Math.Pow returns doubles, not ints.

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

Clojure, 45 bytes

#(apply *(for[i(range 1(inc %))_(range i)]i))

TIO. With one extra byte (use *') you get arbitrary precision.

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

ARBLE, 18 bytes

range(1,n)/(y*z^z)

Simply reduces the range of numbers [1,n] via the function (y,z)=>y*pow(z,z)

Try it online!

\$\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.