Skip to main content
3 of 6
matching brackets
JungHwan Min
  • 13.9k
  • 3
  • 23
  • 69

#Mathematica, 49 bytes

f@2=4;f@1=0;f@n_:=Tr@Range@f[n-1]-Tr@Array[f,n-1]

Defines function f. 1-indexed.

#Explanation

f@2=4;f@1=0;f@n_:=Tr@Range@f[n-1]-Tr@Array[f,n-1]

f@2=4;f@1=0;                                       (* Define f[1] and f[2] *)
            f@n_:=                                 (* f[n] equals ... *)
                  Tr@Range@f[n-1]                  (* Sum of (1, 2, ..., f[n-1]) *)
                                 -Tr@Array[f,n-1]  (* Minus the sum of previous terms *)
JungHwan Min
  • 13.9k
  • 3
  • 23
  • 69