# 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 in the OEIS. Also related are the hyperfactorials, a still quite huge, but a bit smaller sequence: A002109

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 , so the answer with the least length in bytes wins!

• Do we need to consider arbitrarily large integers? Or is it enough to handle the largest that the language's default data type (such as double) supports ? – Luis Mendo Dec 19 '16 at 22:46
• The conversion in-code and output is up to you, the Input will be an integer though. @LuisMendo – devRicher Dec 19 '16 at 22:51
• Changing the rules after many people have answered isn't a nice thing to do either. Please use the Sandbox as advised whenever you want to submit a challenge. – flawr Dec 20 '16 at 9:46

# 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!

• L!DmO also works if you want to take off the "CP-1252 encoding" bit. – Magic Octopus Urn Dec 21 '16 at 21:43

# Mathematica, 19 bytes

Sum[n!^n!,{n,0,#}]&


Apologies for the extremely clear code ;)

• Using mathematica for mathematica - dick move :D – Transcendental Dec 21 '16 at 14:23

# Jelly, 6 bytes

Try it online!

‘Ḷ!*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

• There is an increment atom, ‘, so R!*S‘ saves a byte (I went for ‘Ḷ!*S). – Jonathan Allan Dec 19 '16 at 23:45
• I actually was in the middle of editing before I saw your comment :P – Xanderhall Dec 19 '16 at 23:46
• I saw that too, good job. – Jonathan Allan Dec 19 '16 at 23:46

## R - 34 30 bytes

x=factorial(0:scan());sum(x^x)


vectorizing is nice

edit: saved 4 bytes thanks to @MickyT

• you can shortened it a bit by moving you scan into the factorial x=factorial(0:scan());sum(x^x) – MickyT Dec 21 '16 at 0:19

# 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

• 1#.i.^~@!@,] shaves a few more bytes off. – miles Dec 20 '16 at 1:35
• @miles Oh, cool. I didn't know 1#. performs summation. If that's not a tip already, you should definitely add it! – Conor O'Brien Dec 20 '16 at 1:40

# Perl 6, 4138 37 bytes

{[+] ([**] [*](1..$_)xx 2 for 0..$_)}


{[+] ([**] ([*] 1..$_)xx 2 for 0..$_)}


{sum (($_**$_ with [*] 1..$_) for 0..$_)}


### 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.

• ([*] …) can be written as [*](…) saving a byte – Brad Gilbert b2gills Dec 20 '16 at 3:40

# Cheddar, 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

## 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

• Isn't thank god a little bit presumptuous here? :D – flawr Dec 19 '16 at 23:11
• @flawr ok. Fixed :P – Downgoat Dec 19 '16 at 23:11
• Haha, better, perhaps one of the few legit usages of goat we've seen around here recenly :) – flawr Dec 19 '16 at 23:13

# MATL, 7 bytes

Q:Ygt^s


Try it online!

### 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


# 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 # 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). • I'm not good with Ruby but couldn't you fix this with a=(0..i) instead of a=(1..i)? – Timtech Dec 20 '16 at 15:50 • @Timtech Expect injecting zero into a multiplication doesn't do anyone any good :( – DepressedDaniel Dec 20 '16 at 20:26 • Right, I guess adding +1 is the best solution. – Timtech Dec 20 '16 at 22:43 • @GB Applied trivial fix for n=0 case. – DepressedDaniel Dec 21 '16 at 17:22 # Pyth - 9 8 bytes s^Rd.!Mh  # 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  # Python 2, 73 72 bytes import math lambda n,f=math.factorial:sum(f(i)**f(i)for i in range(n+1))  # PARI/GP, 19 bytes n->sum(k=0,n,k!^k!)  • Was just about to post this. Glad I checked first. – primo Dec 21 '16 at 11:14 # 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. • I think the reason its different than the other answers is because you're supposed to return the sum from 0!^0! to N!^N!. But this is easy to amend by changing to factorialZ(0:x). Is there a specific reason to not use the base::factorial() function? – JAD Dec 20 '16 at 9:47 • @JarkoDubbeldam Thanks for the catch. Need to read the questions better :). I'm using gmp::factorialZ to handle the big numbers. – MickyT Dec 21 '16 at 0:40 ## JavaScript (ES7), 38 bytes f=(n,a=1,i=0)=>i>n?0:a**a+f(n,a*++i,i)  • @fəˈnɛtɪk Sorry, force of habit. – Neil Apr 5 '17 at 12:24 # Golfscript, 41 bytes {.{,{1+}%{*}*}{;1}if}:f;~.,\+{.f\f?}%{+}*  Try it online! ## Pyke, 11 bytes hFSBD]1*B)s  Try it here! 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! ## 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. # JavaScript (ES7), 44 bytes g=n=>n?(f=a=>a?a*f(a-1):1)(n)**f(n)+g(n-1):1  # 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  • Have you tried summing a list comprehension? – Riking Dec 20 '16 at 8:49 • You've only used x once, so you could use range(input()) this would remove a few bytes – george Dec 20 '16 at 21:23 # 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. # 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). # 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--)  # Ruby 2, 41 bytes ->n{(1..n).reduce(s=1){|t,i|t+(s*=i)**s}}  • Awesome! Very clever the way it initializes s at the same time as passing it as the initial t value to reduce/inject. – DepressedDaniel Dec 20 '16 at 20:36 • Can be golfed one more character to ->n{((t=s=1)..n).map{|i|t+=(s*=i)**s};t} or ->n{t=s=1;(1..n).map{|i|t+=(s*=i)**s};t} – DepressedDaniel Dec 20 '16 at 21:04 # Dyalog APL, 10 bytes (+/!*!)0,⍳  How? ⍳ range of input 0, preceded with 0 !*! apply x! ^ x! +/ sum • * and ! are scalar functions, so use arrays: +/*⍨!0,⍳⎕ or (+/!*!)0,⍳ if you really want a train. – Adám Jul 6 '17 at 18:55 # Husk, 7 bytes →ṁo´^Πḣ  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´^ΠΘḣ, actually adding 0 into the range, but since 0!^0!=1^1=1, this works too. # Mathematica, 19 bytes Sum[a!^a!,{a,0,#}]&  Anonymous function. Takes a number as input, and returns a number as output. • We can't use factorial or exponentiation builtin. – Downgoat Dec 19 '16 at 22:32 # Brachylog, 12 bytes y:{$!F:F^}a+


Try it online!

### Explanation

y                 The list [0, ..., Input]
:{      }a       Apply the predicate below to each element of that list
+      The output is the sum of the results

\$!F              F is the factorial of the input
:F^           Output = F^F


# C#, 79 bytes with console output

n=>{int i=1,j=1;for(;i<=n;i++)j*=i;System.Console.Write(System.Math.Pow(j,j));}


# C#, 64 bytes as a return

n=>{int i=1,j=1;for(;i<=n;i++)j*=i;return System.Math.Pow(j,j);}