[Question]
  [
# Ultrafactorials
The ultrafactorials are a sequence of numbers which can be generated using the following function:
$$a(n) = n! ^ {n!}$$
The resulting values rise extremely quickly.
*Side note: This is entry [A046882](https://oeis.org/A046882) in the OEIS. Also related are the hyperfactorials, a still quite huge, but a bit smaller sequence: [A002109](https://oeis.org/A002109)*
# Your task
Your task is to implement these numbers into your language. Your program will calculate the **sum of all ultrafactorials** from 0 up to *inclusive* `n`.
## Input
Your program may only take one input: a number, which resembles the last \$a(n)\$ ultrafactorial to be added to the sum. The Input is assured to be positive or 0.
## Output
Your output is all up to you, as long as there's the visible sum of the numbers somewhere.
## Rules
* You can assume all integers, therefore integer input, and using integer counting loops to produce some results.
## Test cases
```
Input: -1
Output: Any kind of error (because -1! is undefined), or no handling at all
Input: 0
Output: 1
Input: 1
Output: 2
Input: 2
Output: 6
Input: 3
Output: 46662
```
# Challenge
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the answer with the least length in bytes wins!
  
[Answer]
# Mathematica, 19 bytes
```
Sum[n!^n!,{n,0,#}]&
```
Apologies for the extremely clear code ;)
[Answer]
# Jelly, 6 bytes
[Try it online!](http://jelly.tryitonline.net/#code=4oCY4bi2ISpgUw&input=&args=Mw)
```
‘Ḷ!*`S // Main link: Argument n (integer)
‘      // Take n, increment by 1
 Ḷ     // Range from [0..n]
  !    // Calculates factorial for each [0..n]
   *`  // Raises each [0!..n!] to the power of itself
     S // Sum the resulting array
```
[Answer]
# [05AB1E](http://github.com/Adriandmen/05AB1E), 5 bytes
Code:
```
Ý!DmO
```
Explanation:
```
Ý       # Take the range [0, ..., input]
 !      # Map factorial over each element
  Dm    # Exponentiate each element to itself
    O   # Take the sum
```
Uses the **CP-1252** encoding. [Try it online!](http://05ab1e.tryitonline.net/#code=w50hRG1P&input=Mw)
[Answer]
## R -  ~~34~~  30 bytes
```
x=factorial(0:scan());sum(x^x)
```
vectorizing is nice
**edit:** saved 4 bytes thanks to @MickyT
[Answer]
# J, ~~15~~ 12 bytes
Saved 3 bytes thanks to miles!
```
1#.i.^~@!@,]
```
## Explanation
```
1#.i.^~@!@,]  input: y
          ,]  append y to list...
   i.         [0, y)
        !@    factorial each member
     ^~@      raise each to itself
1#.           perform summation
```
## Test cases
```
   f =: 1#.i.^~@!@,]
   (,. f"0) i.4
0     1
1     2
2     6
3 46662
   (,. f"0) i.6
0           1
1           2
2           6
3       46662
4  1.33374e33
5 3.17504e249
   echo"1] _90]\":f 6x
190281878633201797429473437859705759836595323046380462211756876146775419721154680216391116
383660154937824558291984804764687140715927099993629348072943551413397410741069111169123658
220861477766905534108349401724389611558474171816216027733366046875815097164882588181826712
426524007417126023680300953790645455254723360874298622143752208989152655091222094594342956
890526202094068774356589887610542642450567071133028553816930267473112879050178461179814798
008667622200592591542432361632955904924276854403585221477449385731481108378608652069211835
448555831555820393949831627809528917004144455150642180845929102272754394116905511650997561
389917179995442329297103257850695109383021080317204810134810158543814178231002423431556657
737982683316707709406053569620116083909440177269311235173671447595521339849978144493268530
780365729831790064477684808893338190825461650933123545889305523546630119181308584140916288
912561260392366609493077363059677222110731132927863243720195975705161197786520981159422881
575250362836779593393897664990291828935858671453835924398316498051705698128484688847592380
831018330553151156822298060174230201841578757499203145955456593022852288527824268115043999
037373974753999860179933517198889966353093307592136928730661270863274130109304971274296438
682725017433937245229524959283895094220677649257613358344409711070780405579776000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000003175042373780336892901667920556557182493442088021222004926225128381629943118937129
098831435345716937405655305190657814877412786176000000000000000000000000000000000000000000
000000000000000000000000000000000000000000001333735776850284124449081472890438
```
[Answer]
# [Perl 6](http://perl6.org/), ~~41~~ ~~38~~ 37 bytes
```
{[+] ([**] [*](1..$_)xx 2 for 0..$_)}
```
```
{[+] ([**] ([*] 1..$_)xx 2 for 0..$_)}
```
```
{sum (($_**$_ with [*] 1..$_) for 0..$_)}
```
([Try it online.](https://tio.run/#h2mY0))
### Explanation:
* `for 0 .. $_`: For each integer from 0 to the input,
* `[*](1 .. $_) xx 2`: calculate the factorial twice,
* `[**] ...`: and exponentiate the two identical factorials.
* `[+] ...`: Then sum all results of the loop.
*Thanks to b2gills for 1 byte.*
[Answer]
# [Cheddar](http://cheddar.vihan.org), ~~44~~ 37 bytes
```
n->(0|>n=>(i,a=(1|>i)/(*))->a**a)/(+)
```
Thank goats for reduce operator! I think it would've been good idea to add factorial
[Try it online](http://vihan.org/p/tio/?l=cheddar&p=y0ktUUizVcjTtdMwqLHLs7XTyNRJtNUwrLHL1NTX0NLU1LVL1NJKBLK1Na25uAqKMvOAGjSMgRwY20TT@j8A)
## Ungolfed
```
n -> ( 0|>n => (i, a=(1|>i) / (*)) -> a ** a) / (+)
```
## Explanation
**Note:** A little outdated, will fix
```
n ->           // Input
( 0 |> n) =>   // Run below for each of [0, n]
    (              
      i,           // Input
      a =          // Let's keep n! in this variable `a`
         (1 |> i)  // Range from [1, n]
         / (*)     // Multiply all the items of that range
                   // `/` is reduce `(*)` is multiplication function
    ) ->
    a ** a         // A to the power of A
) / (+)        // Sum all results
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), 7 bytes
```
Q:Ygt^s
```
[Try it online!](https://tio.run/nexus/matl#@x9oFZleElf8/78xAA "MATL – TIO Nexus")
### Explanation
```
Q         % Take input n implicitly. Add 1
 :        % Range [1 2 ... n+1]
  Yg      % Gamma function, element-wise. This gives [0! 1! ... n!]
    t^    % Rise result to itself, element-wise
      s   % Sum all values. Implicitly display
```
[Answer]
# PHP, 49 bytes
```
for($f=1;$i<=$argv[1];$f*=++$i)$s+=$f**$f;echo$s;
```
`INF` for `n>5` on a 64 bit system.
**for large numbers, 70 bytes**
```
while($i<=$argv[1])$s=gmp_add($s,gmp_pow($f=gmp_fac($i++),$f));echo$s;
```
requires PHP to be compiled with `--with-gmp`
[Answer]
# Ruby, ~~64~~ 66 bytes
```
->n{(o=(1..n).map{|i|a=(1..i).inject(:*);a**a}.inject(:+))?o+1:1}
```
Added two characters for the off-by-one bugfix (will look into shortening the inject calls later).
[Answer]
# Pyth - ~~9~~ 8 bytes
```
s^Rd.!Mh
```
[Test Suite](http://pyth.herokuapp.com/?code=s%5ERd.%21Mh&test_suite=1&test_suite_input=0%0A1%0A2%0A3&debug=0).
[Answer]
# Haskell, ~~67~~ 56 bytes
Note that this submission was made *before* the rules that banned builtins were removed.
```
p=product
a n=sum[p[x|x<-[p[1..i]],_<-[1..x]]|i<-[0..n]]
```
For example:
```
*Main> a 0
1
*Main> a 1
2
*Main> a 2
6
*Main> a 3
46662
*Main> a 4
1333735776850284124449081472890438
*Main> a 5
3175042373780336892901667920556557182493442088021222004926225128381629943118937129098831435345716937405655305190657814877412786176000000000000000000000000000000000000000000000000000000000000000000000000000000000000001333735776850284124449081472890438
```
[Answer]
# Python 2, ~~73~~ 72 bytes
```
import math
lambda n,f=math.factorial:sum(f(i)**f(i)for i in range(n+1))
```
[Answer]
# [PARI/GP](http://pari.math.u-bordeaux.fr/), 19 bytes
```
n->sum(k=0,n,k!^k!)
```
[Answer]
# GameMaker Language, 97 bytes
Main function (52 bytes)
```
for(a=0;a<=argument0;a++)b+=power(f(a),f(a))return b
```
Function f (45 bytes)
```
a=argument0 if!a return 1else return a*f(a--)
```
[Answer]
# R, 42 ~~35~~ bytes
Now that I've read the question properly, I've put the sum in.
This requires the **gmp** (multiple precision arithmetic) library to be available. This allows for large numbers to be handled. Otherwise anything over 5 returns `INF`.
This is implemented as a unnamed function to avoid the `as.character` that would be required to output to STDOUT through `cat`
```
function(x)sum((i=gmp::factorialZ(0:x))^i)
```
Example run
```
> f <- function(x)sum((i=gmp::factorialZ(0:x))^i)
> f(5)
Big Integer ('bigz') :
[1] 3175042373780336892901667920556557182493442088021222004926225128381629943118937129098831435345716937405655305190657814877412786176000000000000000000000000000000000000000000000000000000000000000000000000000000000000001333735776850284124449081472890438
> f(6)
Big Integer ('bigz') :
[1] 190281878633201797429473437859705759836595323046380462211756876146775419721154680216391116383660154937824558291984804764687140715927099993629348072943551413397410741069111169123658220861477766905534108349401724389611558474171816216027733366046875815097164882588181826712426524007417126023680300953790645455254723360874298622143752208989152655091222094594342956890526202094068774356589887610542642450567071133028553816930267473112879050178461179814798008667622200592591542432361632955904924276854403585221477449385731481108378608652069211835448555831555820393949831627809528917004144455150642180845929102272754394116905511650997561389917179995442329297103257850695109383021080317204810134810158543814178231002423431556657737982683316707709406053569620116083909440177269311235173671447595521339849978144493268530780365729831790064477684808893338190825461650933123545889305523546630119181308584140916288912561260392366609493077363059677222110731132927863243720195975705161197786520981159422881575250362836779593393897664990291828935858671453835924398316498051705698128484688847592380831018330553151156822298060174230201841578757499203145955456593022852288527824268115043999037373974753999860179933517198889966353093307592136928730661270863274130109304971274296438682725017433937245229524959283895094220677649257613358344409711070780405579776000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003175042373780336892901667920556557182493442088021222004926225128381629943118937129098831435345716937405655305190657814877412786176000000000000000000000000000000000000000000000000000000000000000000000000000000000000001333735776850284124449081472890438
```
f(9) will runs pretty well, but fills up a number of pages. A few hundred or so and 2,017,528 digits. f(10) kills the session on my machine.
[Answer]
## JavaScript (ES7), 38 bytes
```
f=(n,a=1,i=0)=>i>n?0:a**a+f(n,a*++i,i)
```
[Answer]
# Dyalog APL, 10 bytes
```
(+/!*!)0,⍳
```
**How?**
`⍳` range of input
`0,` preceded with 0
`!*!` apply `x! ^ x!`
`+/` sum
[Answer]
# [Golfscript](http://www.golfscript.com/golfscript/index.html), 41 bytes
```
{.{,{1+}%{*}*}{;1}if}:f;~.,\+{.f\f?}%{+}*
```
[Try it online!](https://tio.run/##S8/PSStOLsosKPn/v1qvWqfaULtWtVqrVqu22tqwNjOt1irNuk5PJ0a7Wi8tJs0eKKddq/X/vzEA)
[Answer]
# [Thunno](https://github.com/Thunno/Thunno) `+`, \$ 7 \log\_{256}(96) \approx \$ 5.76 bytes
```
RFDz.^S
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72sJKM0Ly9_abSStlLsgqWlJWm6FsuD3Fyq9OKCITyo4IKFxhAGAA)
#### Explanation
```
RFDz.^S  # implicit input
         # + flag adds one
R        # range from 0
 F       # factorial of each
   z.    # for each number:
  D  ^   #  raise it to the power of itself
      S  # sum the resulting list
         # implicit output
```
[Answer]
# [vemf](https://github.com/selaere/vemf), ~~3~~ 5 bytes
```
▲↕!┴^
```
## Explanation
```
▲↕    ' range [0, α)
   ┴  ' run this function:
    ^ '   exponentation
      ' with this for both inputs:
  !   '   factorial of each element in range
```
e.g. `3▲↕!┴^+` – [try it online](https://selaere.github.io/vemf/wasm/index.html?code=Mx4SIcFeKw)
[Answer]
# Mathematica, 19 bytes
```
Sum[a!^a!,{a,0,#}]&
```
Anonymous function. Takes a number as input, and returns a number as output.
[Answer]
## Pyke, 11 bytes
```
hFSBD]1*B)s
```
[Try it here!](http://pyke.catbus.co.uk/?code=hFSBD%5D1%2aB%29s&input=3)
```
hF          -  for i in range (0, input+1)
  SB        -     product(range(1, i+1)
    D]1*    -    [^]*^
        B   -   product(^)
          s - sum(^)
```
Fun fact: Pyke doesn't have a factorial built-in because `SB` is only 2 bytes!
[Answer]
## Haskell, 43 bytes
```
b n|f<-product[1..n]=f^f
a n=sum$b<$>[0..n]
```
Usage example: `a 3`-> `46662`.
`b` calculates a single ultrafactorial and `a` sums all ultrafactorials from `0` to `n`.
[Answer]
# JavaScript (ES7), 44 bytes
```
g=n=>n?(f=a=>a?a*f(a-1):1)(n)**f(n)+g(n-1):1
```
[Answer]
# Python 2, 82 Bytes
```
x=input()
s=0
for z in range(x):
 t=1
 for i in range(z+1):t*=i+1
 s+=t**t
print s
```
[Answer]
# [Wonder](https://github.com/wonderlang/wonder), 33 bytes
```
@sum(->@^ f\prod rng1#0f)rng0+1#0
```
Usage:
```
(@sum(->@^ f\prod rng1#0f)rng0+1#0)3
```
# Explanation
```
rng0+1#0
```
Create inclusive range from 0 to input.
```
->@^ f\prod rng1#0f
```
Map over the range with a function that 1) calculates the factorial of the item, 2) stores the result to `f`, and 3) calculates `f^f`.
```
sum
```
Sum.
[Answer]
# TI-Basic, 13 bytes
```
sum(seq(A!^A!,A,0,Ans
```
P.S. You can replace `sum(seq(` with `Σ(` if you have a newer operating system (no size change).
[Answer]
# Ruby 2, 41 bytes
```
->n{(1..n).reduce(s=1){|t,i|t+(s*=i)**s}}
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 7 bytes
```
→ṁo´^Πḣ
```
[Try it online!](https://tio.run/##AS4A0f9odXNr/yBt4oKB/@KGkuG5gW/CtF7OoOG4o////1swLDEsMiwzLDQsNSw2LDdd "Husk – Try It Online")
```
→          One plus
 ṁ         the sum of
     Π     x!
    ^      to the power of
   ´       itself
 ṁo        for each x in
      ḣ    the range from 1 to
           the input.
```
It would be slightly more correct to use [`ṁo´^ΠΘḣ`](https://tio.run/##AS0A0v9odXNr/yBt4oKB/@G5gW/CtF7OoM6Y4bij////WzAsMSwyLDMsNCw1LDYsN10), actually adding 0 into the range, but since 0!^0!=1^1=1, this works too.
]