25

3

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

2Do 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 – 2016-12-19T22:46:46.170

1The conversion in-code and output is up to you, the Input will be an integer though. @LuisMendo – devRicher – 2016-12-19T22:51:21.270

3

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 – 2016-12-20T09:46:12.190

7

# 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 – 2016-12-21T21:43:12.880

16

# Mathematica, 19 bytes

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


Apologies for the extremely clear code ;)

Using mathematica for mathematica - dick move :D – Transcendental – 2016-12-21T14:23:31.387

8

# 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 – 2016-12-19T23:45:07.523

1I actually was in the middle of editing before I saw your comment :P – Xanderhall – 2016-12-19T23:46:05.003

I saw that too, good job. – Jonathan Allan – 2016-12-19T23:46:49.503

6

## R - 34 30 bytes

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


vectorizing is nice

edit: saved 4 bytes thanks to @MickyT

1you can shortened it a bit by moving you scan into the factorial x=factorial(0:scan());sum(x^x) – MickyT – 2016-12-21T00:19:32.993

4

# 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 – 2016-12-20T03:40:02.427

4

# 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 – 2016-12-20T01:35:53.800

@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 – 2016-12-20T01:40:44.890

3

# 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 – 2016-12-19T23:11:02.977

@flawr ok. Fixed :P – Downgoat – 2016-12-19T23:11:40.570

2Haha, better, perhaps one of the few legit usages of goat we've seen around here recenly :) – flawr – 2016-12-19T23:13:16.407

3

# 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


3

# 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 – 2016-12-20T15:50:46.210

@Timtech Expect injecting zero into a multiplication doesn't do anyone any good :( – DepressedDaniel – 2016-12-20T20:26:42.937

Right, I guess adding +1 is the best solution. – Timtech – 2016-12-20T22:43:19.637

@GB Applied trivial fix for n=0 case. – DepressedDaniel – 2016-12-21T17:22:57.227

3

# 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 2 # 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  2 # Pyth - 9 8 bytes s^Rd.!Mh  2 ## 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 – 2017-04-05T12:24:57.437 2 # 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 – 2016-12-20T09:47:36.733 1@JarkoDubbeldam Thanks for the catch. Need to read the questions better :). I'm using gmp::factorialZ to handle the big numbers. – MickyT – 2016-12-21T00:40:07.800 2 # Python 2, 73 72 bytes import math lambda n,f=math.factorial:sum(f(i)**f(i)for i in range(n+1))  2 # PARI/GP, 19 bytes n->sum(k=0,n,k!^k!)  1Was just about to post this. Glad I checked first. – primo – 2016-12-21T11:14:35.737 1 ## 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! 1 ## 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. 1 # JavaScript (ES7), 44 bytes g=n=>n?(f=a=>a?a*f(a-1):1)(n)**f(n)+g(n-1):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 – 2016-12-20T08:49:15.953 1You've only used x once, so you could use range(input()) this would remove a few bytes – george – 2016-12-20T21:23:50.240 1 # 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. 1 # 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). 1 # 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--)  1 # 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 – 2016-12-20T20:36:02.960 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 – 2016-12-20T21:04:23.580 1 # 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 – 2017-07-06T18:55:25.937 0 # Mathematica, 19 bytes Sum[a!^a!,{a,0,#}]&  Anonymous function. Takes a number as input, and returns a number as output. 1We can't use factorial or exponentiation builtin. – Downgoat – 2016-12-19T22:32:57.023 0 # 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  0 # 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);}  0 # Actually 11 10 bytes 1+r!;ⁿMΣ  How it works Program takes implicit input, implicit print at EOF 1+ Add one to the input n+1 r Create a range (0,1,..,n)   Create a function between the two  ! Factorialize the current stack item ; Duplicate the current stack item ⁿ Power a,b from the current stack item M Map the function across the stack top item Σ Sum the stack together  0 ## Racket 54 bytes (for/sum((i(+ 1 n)))(let((t(factorial i)))(expt t t)))  Ungolfed: #lang racket (require math) (define (f n) (for/sum ((i (+ 1 n))) (let ((t (factorial i))) (expt t t))))  Testing: (f -1) (f 0) (f 1) (f 2) (f 3)  Output: 0 1 2 6 46662  0 # Japt, 8 6 bytes òÊx_pZ  Test it ## Explantion Implicit input of integer U 3 ò  Create an array of integers from 0 to U, inclusive. [0,1,2,3] Ê  Get the factorial of each integer in the array. [1,1,2,6] _  Map over the array. pZ  Raise each element (p) to the power of itself (Z). [1,1,4,46656] x  Reduce by addition and implicitly output the result. 46662 0 # APL (Dyalog), 11 bytes +/(*⍨∘!0,⍳)  Try it online! This function train is equivalent to {+/*⍨!0,⍳⍵}, which is a straight forward implementation 0 # 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. 0 # Perl 6, 38 bytes {[+] map {my \a=[*] 1..$_;a**a},0..\$_}
`

Try it online!

131 bytes – Jo King – 2019-04-22T09:55:52.803