g | x | w | all
Bytes Lang Time Link
029Juby250505T185332ZJordan
009J200316T085421ZGalen Iv
038C gcc200316T235720ZS.S. Ann
015Wolfram Language Mathematica200315T182927ZZaMoC
034Haskell200316T162659ZWheat Wi
005MathGolf200316T144543ZKevin Cr
063C gcc200315T222849ZNoodle9
050Python 2200315T185307ZSurculos
061Factor200316T084444ZGalen Iv
070Racket200316T080227ZGalen Iv
004W j200316T021225Zuser9206
004Jelly200315T182902ZJonathan
005APL Dyalog200315T225912ZJo King
022Perl 6200315T224816ZJo King
034JavaScript ES6200315T185122ZArnauld
012Charcoal200315T203526ZNeil
049Python 3.8200315T191742ZJonathan
005Japt x200315T191146ZGymhgy
057Python 3200315T191556ZRGS
00505AB1E200315T191303ZGrimmy
009Jelly200315T182135Zhyperneu

J-uby, 29 bytes

:+|~:^%(:/&:*|:& &:/|:+&:sum)

Attempt This Online!

J, 9 bytes

1#.!%1+i.

Try it online!

1#.           the sum of (by conversion to base 1)
   !          n factorial
    %         divided by
     1+i.     the list 1..n  

K (oK), 16 12 bytes

-4 bytes thanks to ngn!

+/*/'1+&:'~=

Try it online!

C (gcc), 38 bytes

f(n){n=n>1?(n+n--)*f(n)-n*n*f(n-1):n;}

Port of Jonathan Allan's Python 3.8 answer.

Try it online!

Wolfram Language (Mathematica), 15 bytes

Tr[#!/Range@#]&     

-1 byte from @Greg Martin

Try it online!

Haskell, 34 bytes

a 0=0
a n=n*a(n-1)+product[1..n-1]

Try it online!

This implements the OEIS definition.

MathGolf, 5 bytes

╒k!╠Σ

Try it online.

Or

!k╒/Σ

Try it online.

Explanation:

╒      # Push a list in the range [1, (implicit) input-integer]
 k!    # Push the input-integer again, and pop and push its factorial
   ╠   # Divide the factorial by each value in the list (b/a builtin)
    Σ  # And sum that list
       # (after which the entire stack joined together is output implicitly as result)

!      # Push the factorial of the (implicit) input-integer
 k╒    # Push the input-integer again, and pop and push a list in the range [1, input]
   /   # Divide the factorial by each value in the list
    Σ  # And sum that list
       # (after which the entire stack joined together is output implicitly as result)

C (gcc), 70 \$\cdots\$ 64 63 bytes

Saved a byte thanks to ceilingcat!!!
Saved a byte thanks to Surculose Sputum!!!

l;c;i;t;f(n){l=0;for(c=i=1;i<n;l=c,c=t)t=-l*i*i+c*(i+++i);n=c;}

Try it online!

Uses Arnauld's formula.

Python 2, 57 52 50 bytes

-2 bytes thanks to @JonathanAllan !

i=x=0
f=1
exec"i+=1;x=i*x+f;f*=i;"*input()
print x

Try it online!


If the sequence is 0-indexed, we can cut down 2 more bytes

Python 2, 48 bytes

i=x=f=1
exec"i+=1;x=i*x+f;f*=i;"*input()
print x

Try it online!


How: This solution uses the exec trick, which repeats the code n times, then exec the repeated code.

f is the current factorial.
x (the solution function) is defined by the recurrent relation:
$$x(i)=ix(i-1)+(i-1)!$$
and
$$x(0)=0$$

@RGS's answer has a really nice explanation for this formula.

Factor, 61 bytes

: f ( n -- n ) [1,b] dup [ product ] dip [ / ] with map sum ;

Try it online!

Racket, 70 bytes

(λ(x)(let([a(range 1(+ 1 x))])(apply +(map(λ(y)(/(apply * a)y))a))))

Try it online!

W j, 5 4 bytes

After scanning through the source, I realized an undocumented feature - the j flag can actually sum the output at the end!

7Uëÿ

Uncompressed:

*rak/

Explanation

*r     % Reduce via multiplication
       % (Contains implicit range)
  ak   % Range from input to 1
    /  % Division (integer division when
       % none of the operands are floating-points)
Flag:j % Sum the resulting list

Jelly,  5  4 bytes

!:RS

Try it online! Or see the test-suite.

How?

When we introduce an \$n^{\text{th}}\$ train it allows:

We know \$a(1) = 1\$ so...

n  a(n) 
1  1
2  1! + 1*2
3  2! + 1!*3 + 1*2*3
4  3! + 2!*4 + 1!*3*4 + 1*2*3*4
5  4! + 3!*5 + 2!*4*5 + 1!*3*4*5 + 1*2*3*4*5
... etc

Which is:

n  a(n) 
1  1
2  1 + 2
3  1*2 + 1* 3 + 2*3
4  1*2*3 + 1*2* 4 + 1* 3*4 + 2*3*4
5  1*2*3*4 + 1*2*3* 5 + 1*2* 4*5 + 1* 3*4*5 + 2*3*4*5
...
n  n!/n + n!/(n-1) + n!/(n-2) + ... + n!/1

Hence the code:

!:RS - integer, n
!    - (n) factorial      n!
  R  - range (n)          [1,...,n-2,n-1,n]
 :   - integer division   [n!/1,...,n!/(n-2),n!/(n-1),n!/n]
   S - sum                a(n)

APL (Dyalog), 5 bytes

+/!÷⍳

Try it online!

Calculates the sum of (+/) the factorial of \$n\$ (!) divided by (÷) the range 1 to \$n\$ ().

Perl 6, 22 bytes

{sum [*]($_)X/$_}o^*+1

Try it online!

Returns the sum of the factorial of \$n\$ divided by the range 1 to \$n\$.

JavaScript (ES6), 34 bytes

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

Try it online!

This is an implementation of the recurrence relation:

$$\cases{ a(0)=0\\ a(1)=1\\ a(n) = a(n-1) \times (2n - 1) - a(n-2) \times (n - 1)^2,\:n>1}$$

Charcoal, 12 bytes

≔…·¹NθIΣ÷Πθθ

Try it online! Link is to verbose version of code. Explanation:

≔…·¹Nθ

Create a range from 1 to n.

    θ   Range `1`..`n`
   Π    Product i.e. `n!`
  ÷     Vectorised divide by
     θ  Range `1`..`n`
 Σ      Sum
I       Cast to string
        Implicitly print

Python 3.8,  50  49 bytes

-1 thanks to Surculose Sputum (using walrus in 3.8 to save some ~-s)

f=lambda n:n>1and(2*n-1)*f(n:=n-1)-n*n*f(n-1)or n

Try it online!


A 52 using a different approach:

f=lambda n,k=2:n*k and~-n*f(n-1,k)+f(n-1,k-1)or n==k

Try it online!

Japt -x, 5 bytes

ÆÊ/°X

Try it

Python 3, 57 bytes

f=lambda n:n and n*f(n-1)+math.factorial(n-1)
import math

Try it online!

Implements the recurrence relation given by

$$\begin{cases}a(n) = n\times a(n-1) + (n-1)! \\ a(0) = 0 \end{cases}$$

How: If you have \$n\$ trains on the railway, consider removing the faster one from the railway and take the \$(n-1)!\$ permutations of the remaining \$n-1\$ trains. For each permutation of the \$n-1\$ trains, there are \$n\$ places where you can put the \$n\$th train, the fastest one.

05AB1E, 5 bytes

!IL÷O

Try it online!

Jelly, 9 bytes

Œ!«\€Q€FL

Try it online!

this is the non-smart trivial approach

Explanation

Observe that if we have a list of trains' speeds, moving left, then we can cumulatively reduce by minimum to get the final list of trains' speeds.

Œ!«\€Q€FL  Main Link
Œ!         all permutations (defaults over a range)
    €      For each permutation
   \       Cumulatively reduce by
  «        minimum
      €    For each permutation
     Q     remove duplicate values
       F   join all of the trains (flatten)
        L  and get the final length