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

2
\$\begingroup\$

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.

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

Pari/GP, 18 bytes

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

Try it online!

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

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Gotta remove that whitespace! Also short-circuit operations ended up 1 char shorter: TIO \$\endgroup\$
    – M Virts
    Commented Mar 7, 2022 at 8:08
2
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ 45 bytes \$\endgroup\$
    – alephalpha
    Commented Oct 16, 2021 at 9:10
  • \$\begingroup\$ hacks bruh ill change \$\endgroup\$
    – scpchicken
    Commented Oct 31, 2021 at 17:33
2
\$\begingroup\$

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.

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

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 ×
```
\$\endgroup\$
1
  • 1
    \$\begingroup\$ tacit: ×´·⋆˜1+↕ \$\endgroup\$
    – Razetime
    Commented Feb 3, 2022 at 11:05
2
+100
\$\begingroup\$

Vyxal, 4 bytes

ƛe;Π

Try it Online!

\$\endgroup\$
1
  • \$\begingroup\$ -3 bytes because both n's are taken implicitly \$\endgroup\$
    – math scat
    Commented Mar 3, 2022 at 10:15
2
\$\begingroup\$

Python 3.8 (pre-release), 34 33 bytes

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

Try it online!

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

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.

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

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.

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

Desmos, 19 bytes

f(k)=∏_{n=1}^kn^n

Try It On Desmos!

Try It On Desmos! - Prettified

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

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

Charcoal, 8 bytes

IΠE⊕NXιι

Try it online! Link is to verbose version of code. Explanation:

    N       Input `n` as a number
   ⊕        Incremented
  E         Map over implicit range
      ι     Current value
     X      Raised to power
       ι    Current value
 Π          Product
I           Cast to string
            Implicitly print
\$\endgroup\$
1
\$\begingroup\$

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

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
\$\endgroup\$
1
  • \$\begingroup\$ Also ri),_]ze~:* \$\endgroup\$
    – Luis Mendo
    Commented Oct 4, 2021 at 10:50
1
\$\begingroup\$

Ruby, 24 bytes

f=->n{n<1?1:n**n*f[n-1]}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You are right, sorry. \$\endgroup\$
    – G B
    Commented Oct 4, 2021 at 12:36
1
\$\begingroup\$

Japt, 6 bytes

õ_pZÃ×

Try it here

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

PowerShell, 51 bytes

param($n)iex((1..$n|%{[math]::pow($_,$_)})-join'*')

Try it online!

It is rare to see Powershell answers without spaces

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

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

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

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 16 bytes: !n=n<0||!~-n*n^n \$\endgroup\$ Commented Oct 4, 2021 at 22:31
1
\$\begingroup\$

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:_~>.

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

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.

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

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)))
\$\endgroup\$
3
  • \$\begingroup\$ Hi, I've edited a TIO link into your answer, hope you don't mind. Here's a 62 byte alternative. \$\endgroup\$
    – user
    Commented Oct 4, 2021 at 21:54
  • \$\begingroup\$ 58 bytes using recursion, actually. \$\endgroup\$
    – user
    Commented Oct 4, 2021 at 21:59
  • 1
    \$\begingroup\$ @user should probably post that as a separate answer - read comments on latest edit \$\endgroup\$
    – ASCII-only
    Commented Oct 6, 2021 at 22:26
1
\$\begingroup\$

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

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
    $. *= $_ ** $_
}
$_ = $.
\$\endgroup\$
2
  • \$\begingroup\$ i think this should be 26 bytes as mentioned here codegolf.meta.stackexchange.com/a/14339/104014 \$\endgroup\$
    – scpchicken
    Commented Nov 5, 2021 at 2:11
  • \$\begingroup\$ 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". :( \$\endgroup\$ Commented Nov 5, 2021 at 17:52
1
\$\begingroup\$

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!

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

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.