/* Sum of a range ... (Code golf) in Picat. https://codegolf.stackexchange.com/questions/262647/sum-of-a-range-of-a-sum-of-a-range-of-a-sum-of-a-range-of-a-sum-of-a-range-of-a?page=2&tab=scoredesc#tab-top """ Sum of a range of a sum of a range of a sum of a range of a sum of a range of a sum of Inspired by the fact that a few related challenges to this could be answered by Vyxal in 0 Bytes using a special flag combination. Given only one input integer n, calculate f(n,n) where f(x,y)={xf((∑xk=1k), y−1)if y=0otherwise If you want an explanation in plain English, here it is, quoted from OEIS: Let T(n) be the n-th triangular number n∗(n+1)/2; then a(n) = n-th iteration [of] T(T(T(...(n)))) . Note that a(n) is the function. This is also A099129(n) , but with the case for n=0 . This is code-golf, so as long as you make your answer short, it doesn't matter whether it times out on TIO (my computer can't calculate n=6 within five minutes!). Yes, standard loopholes apply. Test cases: 0 -> 0 1 -> 1 2 -> 6 3 -> 231 4 -> 1186570 5 -> 347357071281165 6 -> 2076895351339769460477611370186681 7 -> 143892868802856286225154411591351342616163027795335641150249224655238508171 """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go ?=> garbage_collect(200_000_000), foreach(N in 1..10) println(N=f(N,N)) end, nl. go => true. f(N) = f(N,N). % a little slower with table. It's just Y iterations since Y is decremented each time. % table % f(X,0) = X. % f(X,Y) = f([K : K in 1..X].sum,Y-1). % very slow % f(X,Y) = f(sum(1..X),Y-1). % still very slow % f(X,Y) = f(X*(X+1)//2,Y-1). % much faster % golfed: 35 chars f(X,0)=X. f(X,Y)=f(X*(X+1)//2,Y-1).