# 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. Commented Oct 5, 2021 at 2:40
• oeis.org/A002109 Commented Nov 14, 2022 at 19:32

# 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 Commented Mar 7, 2022 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 Commented Oct 16, 2021 at 9:10
• hacks bruh ill change Commented Oct 31, 2021 at 17:33

# J-uby, 26 bytes

:+&1|:*|:*&->r{r**r}|:/&:*


Try it online!

I want to remove the inner lambda, but ~:** doesn't seem to work in its place. Might be a bug or I'm missing some argument error.

# BQN, 10 bytes SBCS

{×´⋆˜1+↕𝕩}


Try it!

#### Explanation

{×´⋆˜1+↕𝕩}

{        }   # Anonymous function / block
↕𝕩    # 0..𝕩
1+      # To 1...𝕩
⋆˜        # Raise to the power over itself (1*1,2*2,3*3...𝕩*𝕩)
×´          # Fold using ×
$$$$

• tacit: ×´·⋆˜1+↕ Commented Feb 3, 2022 at 11:05

# Vyxal, 4 bytes

ƛe;Π


Try it Online!

• -3 bytes because both n's are taken implicitly Commented Mar 3, 2022 at 10:15

# Python 3.8 (pre-release), 34 33 bytes

f=lambda n:not(n+1)or n**n*f(n-1)


Try it online!

## PHP 5.6 (50 chars)

Given $argv[1] as a command line argument : $r=($n=$argv[1])**$n;for(;$n--;)$r*=$n**$n;echo$r;


## PHP 5.6 (42 chars)

To get the answer as a variable, the function a($input) return the answer recursively : function a($n){return$n?$n**$n*a($n-1):1;}


## PHP 7 (38 chars)

The function $_ENV is like it's PHP5.6 equivalent function. $_ENV=fn($n)=>$n?$n**$n*$_ENV($n-1):1;


Note : The $_ENV overwriting is a bad practice. # Swift 2.2, 57 bytes In an unexpected twist, I'm putting the old version of Swift nobody uses anymore as my main answer. func h(x:Double)->Double{return x==0 ? 1:pow(x,x)*h(x-1)}  ## Swift >=3.0, 77 bytes In Swift 3, Foundation/Foundation.h is no longer exposed by default, and you have to explicitly import Foundation: import Foundation func h(_ x:Double)->Double{return x==0 ? 1:pow(x,x)*h(x-1)}  ## Swift >=5.1, 70 bytes In Swift 5.1, the return keyword becomes optional: import Foundation func h(_ x:Double)->Double{x==0 ? 1:pow(x,x)*h(x-1)}  TIO doesn't support all these Swift versions, but I found this website which does: SwiftFiddle. # Desmos, 19 bytes f(k)=∏_{n=1}^kn^n  Try It On Desmos! Try It On Desmos! - Prettified # Fig, $$\4\log_{256}(96)\approx\$$ 3.292 bytes r^ua  Try it online! Beats osabie and Vyxal, still longer than Jelly tho. r^ua a # Range [1, n] ^u # Exponentiate by self r # Product  # Charcoal, 8 bytes ＩΠＥ⊕ＮＸιι  Try it online! Link is to verbose version of code. Explanation:  Ｎ Input n as a number ⊕ Incremented Ｅ Map over implicit range ι Current value Ｘ Raised to power ι Current value Π Product Ｉ Cast to string Implicitly print  # MathGolf, 8 bytes ╒_m#ε*1╙  Try it online. Explanation: ╒ # Push a list in the range [1, (implicit) input-integer] _ # Duplicate it m# # Take the exponent of the two lists at the same positions ε* # Reduce this list by multiplication 1╙ # Leave the max of this value and 1 (for edge case input=0) # (after which the entire stack is output implicitly as result)  # CJam, 11 bytes 1ri,{)_#*}%  Try it online! ### How it works 1 e# Push 1 ri e# Read input and intepret as an integer, n , e# Range. Gives [0 1 2 -... n-1] { }% e# For each k in the range ) e# Add 1 _ e# Duplicate # e# Power * e# Product e# Implicit display  • Also ri),_]ze~:* Commented Oct 4, 2021 at 10:50 # Ruby, 24 bytes f=->n{n<1?1:n**n*f[n-1]}  Try it online! • You are right, sorry. – G B Commented Oct 4, 2021 at 12:36 # Japt, 6 bytes õ_pZÃ×  Try it here # PowerShell, 51 bytes param($n)iex((1..$n|%{[math]::pow($_,$_)})-join'*')  Try it online! It is rare to see Powershell answers without spaces • Try it online? Commented Oct 4, 2021 at 19:58 # Scala, 43 bytes 1.to(_)./:(1)((a,n)=>a*math.pow(n,n).toInt)  Try it in Scastie! For comparison, the recursive solution is 56 bytes: def f(n:Int):Int=if(n>0)math.pow(n,n).toInt*f(n-1)else 1  # Pip, 9 bytes Y\,a$*yEy


Takes input via command-line argument. Try it here! Or, here's a 10-byte equivalent in Pip Classic: Try it online!

### Explanation

 \,a       Inclusive range from 1 to command-line arg
Y          Yank that into y
yEy  y to the power of y
$* Fold on multiplication  # Julia 1.0, 17 16 bytes !n=n<0||!~-n*n^n  Try it online! -1 byte thanks to dingledooper - I always forget about ~-n • 16 bytes: !n=n<0||!~-n*n^n Commented Oct 4, 2021 at 22:31 # Attache, 14 bytes Prod@{1:_^1:_}  Try it online! Composes the Product function with a function which computes k^k for k in 1..n. Slightly more elegant, 15 bytes: Prod@{_^_}@1&: Less characters, more bytes (13ch/15b): Prod«1:_^1:_», or the equivalent 15b program Prod<~1:_^1:_~>. # Java (JDK), 51 bytes double a(int i){return i<1?1:Math.pow(i,i)*a(i-1);}  Try it online! returning a double saves bytes as you don't have the cast the Math.pow Same byte-count as the other java answer but using recursion. # Clojure, 104 86 bytes (defn h[n](->>(rest(range(inc n)))(map #(repeat %1 %1))(map #(apply *' %))(apply *')))  Try this version online! Cut it down to 86 bytes by using the thread-after macro. Un-golfed: (defn hyperfactorial [n] (->> (rest (range (inc n))) (map #(repeat %1 %1)) (map #(apply *' %)) (apply *')))  Previous 104 byte answer: (defn h[n](let[p(fn[c](apply *' c))m(rest(range(inc n)))f(map #(repeat %1 %1)m)q(map p f)](apply *' q)))  Try it online! Ungolfed: (defn hyperfactorial [n] (let [ pow (fn [c] (apply *' c)) nums (rest (range (inc n))) ; '(1 2 3 ... n) factors (map #(repeat %1 %1) nums) powers (map pow factors) ] (apply *' powers)))  • Hi, I've edited a TIO link into your answer, hope you don't mind. Here's a 62 byte alternative. – user Commented Oct 4, 2021 at 21:54 • 58 bytes using recursion, actually. – user Commented Oct 4, 2021 at 21:59 • @user should probably post that as a separate answer - read comments on latest edit Commented Oct 6, 2021 at 22:26 # O, 14 bytes [[j,;]{nn^}d]*  Try it online! [ # Open the array [j,;] # Input range 1..n {nn^} # Push a block to power by self d] # Apply n close * # Product  # Perl 5-p, 27 bytes 26 bytes for code; +1 byte for -p switch $.*=$_**$_ for 1..$_;$_=$.  Try it online! Ungolfed (but somehow not any more readable): for (1..$_) {
# The built-in variable $. ($INPUT_LINE_NUMBER) defaults to 1
$. *=$_ ** $_ }$_ = $.  • i think this should be 26 bytes as mentioned here codegolf.meta.stackexchange.com/a/14339/104014 Commented Nov 5, 2021 at 2:11 • Most of my answers end up being in Perl so this would definitely benefit me. However, the general consensus right now seems to be "add the byte count of interpreter flags other than -e". :( Commented Nov 5, 2021 at 17:52 # Raku, 26 bytes {[*] (0..*Z**0..*)[1..$_]}

      0..*                  # range from 0 to infinity
0..*Z**0..*           # zip with exponent
(0..*Z**0..*)[1..$_] # slice from range 1 to default variable (var passed to function) inclusive [*] (0..*Z**0..*)[1..$_]   # reduce on multiplication
{[*] (0..*Z**0..*)[1..$_]} # block  Try it online! # 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! # 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

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

# Red, 40 bytes

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


Try it online!