Skip to main content
2 of 3
improvement
adrianmp
  • 1.7k
  • 1
  • 10
  • 15

#C#, 67 65 bytes#

f=n=>{int x=n;var r="";while(n>0)r+=n--;return x>0?f(x-1)+r:"";};

Anonymous recursive function which returns the required string.

Full program with ungolfed method and test cases:

using System;

public class Program
{
    public static void Main()
    {
        Func<int, string> f = null;
        f = n =>
        {
            int x = n;
            var r = "";
            while (n > 0)
                r += n--;
            return x > 0 ? f(x - 1) + r : "";
        };

        Console.WriteLine(f(6));    // 121321432154321654321
        Console.WriteLine(f(1));    // 1
        Console.WriteLine(f(26));   // 121321432154321654321765432187654321987654321109876543211110987654321121110987654321131211109876543211413121110987654321151413121110987654321161514131211109876543211716151413121110987654321181716151413121110987654321191817161514131211109876543212019181716151413121110987654321212019181716151413121110987654321222120191817161514131211109876543212322212019181716151413121110987654321242322212019181716151413121110987654321252423222120191817161514131211109876543212625242322212019181716151413121110987654321
        Console.WriteLine(f(0));    // "" (empty string)
    }
}
adrianmp
  • 1.7k
  • 1
  • 10
  • 15