15
\$\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   4.0310784*10^12
7   3.3197664*10^18
8   5.5696438*10^25

Rules

\$\endgroup\$
1
  • \$\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
    Oct 5 at 2:40

43 Answers 43

12
\$\begingroup\$

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
\$\endgroup\$
5
  • \$\begingroup\$ I will accept this answer since it appears to be the shortest. \$\endgroup\$
    – Nirvana
    Oct 4 at 15:52
  • 9
    \$\begingroup\$ @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. \$\endgroup\$ Oct 4 at 17:15
  • \$\begingroup\$ Jelly will still be the winner, 3 bytes is damn short \$\endgroup\$
    – wasif
    Oct 4 at 17:55
  • 3
    \$\begingroup\$ Tied with Gaia now. \$\endgroup\$
    – Adám
    Oct 4 at 20:38
  • \$\begingroup\$ @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 \$\endgroup\$ Oct 6 at 14:00
8
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

APL (Dyalog Classic), 5 bytes

⍳×.*⍳

Try it online!

Monadic fork train

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

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

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!

\$\endgroup\$
1
  • \$\begingroup\$ The built-in Hyperfactorial is 14 bytes \$\endgroup\$
    – att
    Oct 4 at 19:27
4
\$\begingroup\$

Husk, 5 bytes

Πm´^ḣ

Try it online!

Π       # product of
 m      # mapping across all values of
     ḣ  # 1..input
  ´^    # x to the power of x
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES7), 20 bytes

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

Try it online!

\$\endgroup\$
4
\$\begingroup\$

R, 20 bytes

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

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Haskell, 20 bytes

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

Try it online!

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Golf! Nice answer! \$\endgroup\$ Oct 4 at 18:46
4
\$\begingroup\$

Java (JDK), 51 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Factor + math.unicode, 33 27 bytes

[ [1,b] [ dup ^n ] map Π ]

Try it online!

This could have been 26 bytes, but ^ returns NaN for 0^0. Factor is the only language I know of that doesn't just return 1 for that. So I had to use ^n instead, which is a helper word used to implement ^ that hasn't been 'fixed' yet.

  • [1,b] A range from 1 to whatever is on top of the data stack, inclusive.
  • dup ^n Raise an integer to itself.
  • [ ... ] map Apply a quotation to each element of a sequence, collecting the results in a new sequence of the same length.
  • Π Product of a sequence.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can add Mathematica to that list :) \$\endgroup\$
    – att
    Oct 5 at 19:59
3
\$\begingroup\$

Python 3, 28 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

C (gcc), 28 bytes

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

Try it online!

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

05AB1E, 4 bytes

LDmP

Try it online or verify all test cases.

Explanation:

L     # Push a list in the range [1, (implicit) input-integer]
      # (note: input 0 will create the list [1,0])
 D    # Duplicate this list
  m   # Take the exponent of the two lists at the same positions
   P  # Take the product of this list
      # (after which the result is output implicitly)
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ wow reminds me a lot of rust \$\endgroup\$
    – don bright
    Oct 5 at 4:33
2
\$\begingroup\$

jq, 42 bytes

[range(.+1)]|reduce.[]as$i(1;.*pow($i;$i))

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can simplify the [range]|reduce.[] to reduce(range(.+1))as$i to save 4 bytes] \$\endgroup\$
    – Sara J
    Oct 6 at 18:27
2
\$\begingroup\$

Vyxal, 4 bytes

ɾ:eΠ

Try it Online!

Obligatory answer

ɾ              # Range
:              # Dup
e             # Power
Π            # Product
\$\endgroup\$
2
\$\begingroup\$

MATL, 4 bytes

:t^p

Try it online!

First MATL answer!

Uses the same range-dup-power-product

\$\endgroup\$
3
  • 1
    \$\begingroup\$ First MATL answer and a very nice one! \$\endgroup\$
    – Luis Mendo
    Oct 4 at 15:17
  • 1
    \$\begingroup\$ @LuisMendo thank you! MATL is a quite nice golfing language \$\endgroup\$
    – wasif
    Oct 4 at 15:59
  • 1
    \$\begingroup\$ It looks like some kind of emoticon, but I can't figure out what's happening with the eyes... \$\endgroup\$
    – DLosc
    Oct 4 at 22:11
2
\$\begingroup\$

Zsh, 33 bytes

for ((a=1;n++<$1;a*=n**n)):
<<<$a

Attempt This Online!

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

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.

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

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

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

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

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!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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