25
\$\begingroup\$

This question was reworked, please re-read it.

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

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!

\$\endgroup\$
  • 2
    \$\begingroup\$ 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 ? \$\endgroup\$ – Luis Mendo Dec 19 '16 at 22:46
  • 1
    \$\begingroup\$ The conversion in-code and output is up to you, the Input will be an integer though. @LuisMendo \$\endgroup\$ – devRicher Dec 19 '16 at 22:51
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – flawr Dec 20 '16 at 9:46

34 Answers 34

7
\$\begingroup\$

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!

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

Mathematica, 19 bytes

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

Apologies for the extremely clear code ;)

\$\endgroup\$
  • \$\begingroup\$ Using mathematica for mathematica - dick move :D \$\endgroup\$ – Transcendental Dec 21 '16 at 14:23
8
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ There is an increment atom, , so R!*`S‘ saves a byte (I went for ‘Ḷ!*`S). \$\endgroup\$ – Jonathan Allan Dec 19 '16 at 23:45
  • 1
    \$\begingroup\$ I actually was in the middle of editing before I saw your comment :P \$\endgroup\$ – Xanderhall Dec 19 '16 at 23:46
  • \$\begingroup\$ I saw that too, good job. \$\endgroup\$ – Jonathan Allan Dec 19 '16 at 23:46
6
\$\begingroup\$

R - 34 30 bytes

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

vectorizing is nice

edit: saved 4 bytes thanks to @MickyT

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

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
\$\endgroup\$
  • \$\begingroup\$ 1#.i.^~@!@,] shaves a few more bytes off. \$\endgroup\$ – miles Dec 20 '16 at 1:35
  • \$\begingroup\$ @miles Oh, cool. I didn't know 1#. performs summation. If that's not a tip already, you should definitely add it! \$\endgroup\$ – Conor O'Brien Dec 20 '16 at 1:40
4
\$\begingroup\$

Perl 6, 41 38 37 bytes

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

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

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

(Try it online.)

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.

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

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
\$\endgroup\$
  • \$\begingroup\$ Isn't thank god a little bit presumptuous here? :D \$\endgroup\$ – flawr Dec 19 '16 at 23:11
  • \$\begingroup\$ @flawr ok. Fixed :P \$\endgroup\$ – Downgoat Dec 19 '16 at 23:11
  • 2
    \$\begingroup\$ Haha, better, perhaps one of the few legit usages of goat we've seen around here recenly :) \$\endgroup\$ – flawr Dec 19 '16 at 23:13
3
\$\begingroup\$

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

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

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

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

\$\endgroup\$
  • \$\begingroup\$ I'm not good with Ruby but couldn't you fix this with a=(0..i) instead of a=(1..i)? \$\endgroup\$ – Timtech Dec 20 '16 at 15:50
  • \$\begingroup\$ @Timtech Expect injecting zero into a multiplication doesn't do anyone any good :( \$\endgroup\$ – DepressedDaniel Dec 20 '16 at 20:26
  • \$\begingroup\$ Right, I guess adding +1 is the best solution. \$\endgroup\$ – Timtech Dec 20 '16 at 22:43
  • \$\begingroup\$ @GB Applied trivial fix for n=0 case. \$\endgroup\$ – DepressedDaniel Dec 21 '16 at 17:22
2
\$\begingroup\$

Pyth - 9 8 bytes

s^Rd.!Mh

Test Suite.

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

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

Python 2, 73 72 bytes

import math
lambda n,f=math.factorial:sum(f(i)**f(i)for i in range(n+1))
\$\endgroup\$
2
\$\begingroup\$

PARI/GP, 19 bytes

n->sum(k=0,n,k!^k!)
\$\endgroup\$
  • 1
    \$\begingroup\$ Was just about to post this. Glad I checked first. \$\endgroup\$ – primo Dec 21 '16 at 11:14
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – JAD Dec 20 '16 at 9:47
  • 1
    \$\begingroup\$ @JarkoDubbeldam Thanks for the catch. Need to read the questions better :). I'm using gmp::factorialZ to handle the big numbers. \$\endgroup\$ – MickyT Dec 21 '16 at 0:40
2
\$\begingroup\$

JavaScript (ES7), 38 bytes

f=(n,a=1,i=0)=>i>n?0:a**a+f(n,a*++i,i)
\$\endgroup\$
  • \$\begingroup\$ @fəˈnɛtɪk Sorry, force of habit. \$\endgroup\$ – Neil Apr 5 '17 at 12:24
1
\$\begingroup\$

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!

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

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.

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

JavaScript (ES7), 44 bytes

g=n=>n?(f=a=>a?a*f(a-1):1)(n)**f(n)+g(n-1):1
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Have you tried summing a list comprehension? \$\endgroup\$ – Riking Dec 20 '16 at 8:49
  • 1
    \$\begingroup\$ You've only used x once, so you could use range(input()) this would remove a few bytes \$\endgroup\$ – george Dec 20 '16 at 21:23
1
\$\begingroup\$

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.

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

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

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

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

Ruby 2, 41 bytes

->n{(1..n).reduce(s=1){|t,i|t+(s*=i)**s}}
\$\endgroup\$
  • \$\begingroup\$ Awesome! Very clever the way it initializes s at the same time as passing it as the initial t value to reduce/inject. \$\endgroup\$ – DepressedDaniel Dec 20 '16 at 20:36
  • \$\begingroup\$ 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} \$\endgroup\$ – DepressedDaniel Dec 20 '16 at 21:04
1
\$\begingroup\$

Dyalog APL, 10 bytes

(+/!*!)0,⍳

How?

range of input

0, preceded with 0

!*! apply x! ^ x!

+/ sum

\$\endgroup\$
  • \$\begingroup\$ * and ! are scalar functions, so use arrays: +/*⍨!0,⍳⎕ or (+/!*!)0,⍳ if you really want a train. \$\endgroup\$ – Adám Jul 6 '17 at 18:55
0
\$\begingroup\$

Mathematica, 19 bytes

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

Anonymous function. Takes a number as input, and returns a number as output.

\$\endgroup\$
  • 1
    \$\begingroup\$ We can't use factorial or exponentiation builtin. \$\endgroup\$ – Downgoat Dec 19 '16 at 22:32
0
\$\begingroup\$

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

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);}
\$\endgroup\$
0
\$\begingroup\$

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

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