fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. PrintPyramid(4);
  8. }
  9.  
  10. public static void PrintPyramid(int n)
  11. {
  12. var i = 0;
  13.  
  14. while (n > 0)
  15. {
  16. Console.Write(++i);
  17.  
  18. if (IsTriangularNumber(i))
  19. {
  20. Console.Write(Environment.NewLine);
  21. n--;
  22. }
  23. else
  24. {
  25. Console.Write(" ");
  26. }
  27. }
  28. }
  29.  
  30. public static bool IsTriangularNumber(int i)
  31. {
  32. var n = (int)Math.Sqrt(i*2);
  33.  
  34. return n*(n + 1) / 2 == i;
  35. }
  36. }
Success #stdin #stdout 0.03s 23880KB
stdin
Standard input is empty
stdout
1
2 3
4 5 6
7 8 9 10