| Bytes | Lang | Time | Link |
|---|---|---|---|
| 029 | Juby | 250505T185332Z | Jordan |
| 009 | J | 200316T085421Z | Galen Iv |
| 038 | C gcc | 200316T235720Z | S.S. Ann |
| 015 | Wolfram Language Mathematica | 200315T182927Z | ZaMoC |
| 034 | Haskell | 200316T162659Z | Wheat Wi |
| 005 | MathGolf | 200316T144543Z | Kevin Cr |
| 063 | C gcc | 200315T222849Z | Noodle9 |
| 050 | Python 2 | 200315T185307Z | Surculos |
| 061 | Factor | 200316T084444Z | Galen Iv |
| 070 | Racket | 200316T080227Z | Galen Iv |
| 004 | W j | 200316T021225Z | user9206 |
| 004 | Jelly | 200315T182902Z | Jonathan |
| 005 | APL Dyalog | 200315T225912Z | Jo King |
| 022 | Perl 6 | 200315T224816Z | Jo King |
| 034 | JavaScript ES6 | 200315T185122Z | Arnauld |
| 012 | Charcoal | 200315T203526Z | Neil |
| 049 | Python 3.8 | 200315T191742Z | Jonathan |
| 005 | Japt x | 200315T191146Z | Gymhgy |
| 057 | Python 3 | 200315T191556Z | RGS |
| 005 | 05AB1E | 200315T191303Z | Grimmy |
| 009 | Jelly | 200315T182135Z | hyperneu |
J, 9 bytes
1#.!%1+i.
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+&:'~=
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.
MathGolf, 5 bytes
╒k!╠Σ
Or
!k╒/Σ
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;}
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
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
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.
Racket, 70 bytes
(λ(x)(let([a(range 1(+ 1 x))])(apply +(map(λ(y)(/(apply * a)y))a))))
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:
- \$(n-1)!\$ states - by being placed behind none of the \$n-1\$ existing trains and being faster than all of them.
- all the previous end states, each in \$n\$ different ways - by being placed behind at least one existing train and being faster than \$[0,n-1]\$ of the \$n-1\$ existing trains
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
+/!÷⍳
Calculates the sum of (+/) the factorial of \$n\$ (!) divided by (÷) the range 1 to \$n\$ (⍳).
Perl 6, 22 bytes
{sum [*]($_)X/$_}o^*+1
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
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
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
Python 3, 57 bytes
f=lambda n:n and n*f(n-1)+math.factorial(n-1)
import math
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.
- If you put it in the front, then the connections made by the other \$n-1\$ trains remain exactly the same, and those never catch up with the faster train. So placing it in the front adds a new train to the total sum.
- If you put it in any other of the \$n - 1\$ positions, it will just connect to the train in front and then we are back at the case when we had \$n - 1\$ trains on the railway, so the total number of trains stays the same.
Jelly, 9 bytes
Œ!«\€Q€FL
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