# 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

• I think one might be able to write a competitive 4 bit assembler (or even 8 bit assembler) answer which is a tiny LUT. Oct 5, 2021 at 2:40

# Jelly, 3 bytes

*)P


Try it online!

## How it works

*)P - Main link. Takes n on the left
)  - Over each integer 1 ≤ i ≤ n:
*   -   Raise i to the power i
P - Product

• I will accept this answer since it appears to be the shortest. Oct 4, 2021 at 15:52
• @Nirvana I appreciate that. However, 8 hours is far too short to accept a winner, as it often indicates (unofficially) that the challenge is over. If I accept a winner, I usually wait a minimum of 3 days so that everyone has time to see the challenge. Additionally, we discourage accepting an answer on pure [code-golf] challenges, as we consider these challenges to be competitions within languages (e.g. Jelly vs Jelly, rather than Jelly vs Java), and accepting an answer contradicts that. It's entirely up to you if you'd like to follow that convention however. Oct 4, 2021 at 17:15
• Jelly will still be the winner, 3 bytes is damn short Oct 4, 2021 at 17:55
• Tied with Gaia now.
Oct 4, 2021 at 20:38
• @cairdcoinheringaahing see, and THAT is why we have the occasional "ban golflangs" thread on meta - you can say how things should be and stuff but this is reality Oct 6, 2021 at 14:00

# Gaia, 3 bytes

*†Π


Try it online!

† vectorizes the operator on its left, integer arguments are implicitly cast to ranges. * is exponentiation and Π the product of a list.

# Wolfram Language (Mathematica), 19 bytes

Product[n^n,{n,#}]&


Try it online!

also as @att mentioned there is a built-in for this...

# Wolfram Language (Mathematica), 14 bytes

Hyperfactorial


Try it online!

• The built-in Hyperfactorial is 14 bytes
– att
Oct 4, 2021 at 19:27

# Python 3, 28 bytes

f=lambda n:n<1or n**n*f(n-1)


Try it online!

# APL (Dyalog Classic), 5 bytes

⍳×.*⍳


Try it online!

First it evaluates range of the input, and dots the power and takes the product.

• While not as nice looking, ×.*⍨⍳ would be more efficient.
Oct 5, 2021 at 5:36
• @Adám nice, i wasn't aware of this use of commute Oct 8, 2021 at 9:19

# JavaScript (ES7), 20 bytes

f=n=>n?n**n*f(n-1):1


Try it online!

# tinylisp, 51 bytes

(load library
(d H(q((N)(i N(*(pow N N)(H(dec N)))1


Try it online!

## Ungolfed

(load library)            ; for *, pow, and dec

(def hyper                ; name something
(lambda (n)             ; a lambda function that takes one parameter n
(if n                 ; if n...
(*                  ; ...is non-zero, multiply...
(pow n n)         ; ...n raised to itself...
(hyper (dec n)))  ; by the hyperfactorial of n-1
1)))                ; otherwise if n is zero, return 1

• 50 bytes non-recursive solution Feb 3 at 13:22

# Husk, 5 bytes

Πm´^ḣ


Try it online!

Π       # product of
m      # mapping across all values of
ḣ  # 1..input
´^    # x to the power of x


# R, 20 bytes

prod((x=1:scan())^x)


Try it online!

h 0=1
h n=n^n*h(n-1)


Try it online!

# Kotlin, 59 bytes

fun h(i:Double):Double=if(i<1)1.0 else Math.pow(i,i)*h(i-1)


Try it online!

Longest submission yet, but thought I might as well give Kotlin a shot. Used the = format for functions to eliminate 2 brackets as well as the return.

• Welcome to Code Golf! Nice answer! Oct 4, 2021 at 18:46

# Java (JDK), 51 bytes

n->{var r=1;for(;n>0;)r*=Math.pow(n,n--);return r;}


Try it online!

# TI-Basic, 19 17 14 bytes

prod(seq(I^I,I,1,Ans+not(Ans


sadly 0^0 or an empty list errors, so 0 has to be turned into 1

# Nibbles 2 bytes

$*@^  Note that this problem influenced how nibbles decides to use implicit args from implicit ops. I'm currently thinking of getting rid of implicit fold since it is uncommonly used though.  # implicit fold since accumulator is used # implicit range from 1 since there's things after an integer$    # first input integer
*   # mult
@  # accumulator from fold
^ # exponentiation
# implicit $since need another arg to ^ # implicit$ since need another arg to ^


# C (gcc), 28 bytes

H(n){n=n?pow(n,n)*H(--n):1;}


Try it online!

# Wren, 43 bytes

var F=Fn.new{|n|n<1?1:n.pow(n)*F.call(n-1)}


Try it online!

Alternate:

# Wren, 44 bytes

Fn.new{|n|(1..n).reduce(1){|a,b|a*b.pow(b)}}


Try it online!

• wow reminds me a lot of rust Oct 5, 2021 at 4:33

<<<$a Attempt This Online! # Python 3.8 (pre-release), 59 bytes from math import* lambda n:prod(i**i for i in range(1,n+1))  Try it online! # Raku, 17 bytes {[*] [\R*] 1..$_}


Try it online!

• 1 .. $_ is the sequence of numbers from 1 to the input argument. • [\R*] is the "triangular reduction" (scan) of those numbers with the Reversed (and thus right-associative) multiplication operator, producing this list: 𝑛, 𝑛 × (𝑛-1), 𝑛 × (𝑛-1) × (𝑛-2), ..., 𝑛 × (𝑛-1) × (𝑛-2) × ... × 1. • [*] is the product of those numbers, which contains 𝑛 factors of 𝑛, 𝑛-1 factors of 𝑛-1, etc, as required. If the input argument is zero, 1 ..$_ is an empty range, [\R*] applied to that range produces an empty list, and [*] applied to that list returns the multiplicative identity element 1.

# TI-BASIC, 13 bytes

(TI-84+ only, for randIntNoRep()

max(1,randIntNoRep(1,Ans
prod(Ans^Ans


Takes input as the last expression entered, like so:

0:prgmH
1
2:prgmH
4
4:prgmH
27648
5:prgmH
86400000


NB: TI-BASIC is a tokenized language; that means the textual representation of bytes can be multiple characters. In fact, e.g., randIntNoRep( is a single character that cannot be edited as one might edit an ASCII string. This program is represented by the following hex bytes (if I translated it right):

19 31 2B EF 35 31 2B 72 3F B7 72 F0 72


Based off of this site. All tokens are one byte, with the exception of randIntNoRep(, which is represented with two bytes, EF 35. 3F represents the newline.

## Explanation

randIntNoRep(1,Ans returns a range of numbers from 1 to Ans. This is, as far as I can tell, the shortest way to generate a range. This occupies 5 bytes. For Ans = 0, we get either {0, 1} or {1, 0} depending on the RNG. Since TI-BASIC errors out when trying to compute $$\0^0\$$, we need to remove the zeroes by taking the max of each element with 1 (max(1,. Even though we compute 1^1 twice for Ans = 0, this does not affect the overall product.

Next, we simply compute the hyperfactorial factors as Ans^Ans, raising the list to the power of itself. After, we take the prod( of this list, returning a single number.

# Pari/GP, 18 bytes

n->prod(i=1,n,i^i)


Try it online!

# Python 3, 47 41 bytes

new code

z=lambda k,a=1: z(k-1,a*k**k) if k else a


old code (for python 3.8)

lambda k,a=1:[a:=a*i**i for i in range(k+1)][k]


Try it here!

Explanation: Looping through and getting each element, we assign and insert with the walrus operator (I love this thing haha), and return the last element (Which, here, is k saving one more byte instead of using -1!). I am also making use of the default value for the lambda to assign 1 to a, since I can't assign it inside the lambda. Note that i lose 2 bytes in range(k+1) since the range doesnt go to the last number, but I don't see a way around it.

Getting rid of both the walrus (rip) and the range (yes!), it is done with a recursive lambda, saving a whole 6 bytes! We multiply the values and store the result, which we send to the next iteration of the code until k = 1, when we can just ignore and return a since 1^1*a = a.

• Gotta remove that whitespace! Also short-circuit operations ended up 1 char shorter: TIO Mar 7 at 8:08

# Rust, 57 45 bytes

@alephalpha gr8 m8 8 outta 8 (they use fold/reduce with a clojure closure instead of recursion to avoid function boilerplate)

|n:i128|(1..n).fold(1,|p,i|i.pow(i as u32)*p)


Try it online!

oeuf

• 45 bytes Oct 16, 2021 at 9:10
• hacks bruh ill change Oct 31, 2021 at 17:33